微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

在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.jb51.cc/angularjs/140561.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐