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

在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);
    }
}

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...