Sas*_*sxa 2 typescript angular
目前没有任何代码,但我一直在尝试使用标准/样板语法,Injector 如下所示。当我resolveAndCreate()一个服务(也尝试了其他注入器方法...)时,会创建新实例并覆盖引导实例。
预期用例是与@CanActivate()装饰器一起使用。我一直在做糟糕的解决方法(-:window["__ready__"]在服务准备好时进行设置并在装饰器中使用它......
一种解决方案是将引导程序中创建的注入器的引用存储在专用的单例中。
bootstrap(App, [Auth, ROUTER_PROVIDERS])
.then((appRef: ComponentRef) => {
// store a reference to the injector
appInjector(appRef.injector);
});
Run Code Online (Sandbox Code Playgroud)
单例如下:
let appInjectorRef: Injector;
export const appInjector = (injector?: Injector):Injector => {
if (injector) {
appInjectorRef = injector;
}
return appInjectorRef;
};
Run Code Online (Sandbox Code Playgroud)
然后你就可以像这样访问该服务:
let injector: Injector = appInjector(); // get the stored reference to the injector
let auth: Auth = injector.get(Auth); //get the service from the injector
Run Code Online (Sandbox Code Playgroud)
这是一个 Plunker,您可以在其中看到它的实际效果。(我不是 plunker 的作者,但这对我真的很有帮助):http://plnkr.co/edit/SF8gsYN1SvmUbkosHjqQ ?p=preview
| 归档时间: |
|
| 查看次数: |
6608 次 |
| 最近记录: |