角度 – 存储用于组件的注射器实例

前端之家收集整理的这篇文章主要介绍了角度 – 存储用于组件的注射器实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在RC5之前,我正在使用像注射器这样的服务定位器:

Startup.ts

bootstrap(...)
.then((appRef: any) => {
    ServiceLocator.injector = appRef.injector;
});

ServiceLocator.ts

export class ServiceLocator {
    static injector: Injector;
}

组件:

let myServiceInstance = <MyService>ServiceLocator.injector.get(MyService)

现在在bootstrapModule()中做同样的事情then()不起作用,因为组件似乎在承诺之前开始执行.

在组件加载之前有没有办法存储注入器实例?

我不想使用构造函数注入,因为我使用由许多组件导出的基本组件中的注入器,而不是将注入器注入到所有组件中.

我已经设法使用手动吹嘘.不要在@NgModule中使用“bootstrap:[AppComponent]”声明,而是使用ngDoBootstrap方法
export class AppModule {
    constructor(private injector: Injector) {
    }

    ngDoBootstrap(applicationRef: ApplicationRef) {
        ServiceLocator.injector = this.injector;
        applicationRef.bootstrap(AppComponent);
    }
}
原文链接:https://www.f2er.com/angularjs/140561.html

猜你在找的Angularjs相关文章