相关疑难解决方法(0)

使用全局注入器的Angular 7组件继承

我一直在使用MSDN博客文章中描述的方法来简化扩展组件,而无需在super()调用中提供所有依赖关系。但是,这已停止在Type 7的Angular 7中工作。

因此,发生的事情是在引导后,我试图将注射器存储在服务中,然后再尝试对其进行检索。

platformBrowserDynamic().bootstrapModule(AppModule).then(ref => {
  // Store module's injector in the AppInjector class
  console.log('Expected #1: storing app injector');
  AppInjector.setInjector(ref.injector);
})
Run Code Online (Sandbox Code Playgroud)

然后在基本组件中提取存储的注射器

export class BaseComponent {
  constructor() {
    console.log('Expected #2: retrieving stored injector');
    const injector = AppInjector.getInjector();
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,从控制台BaseComponent来看,顺序是相反的-首先调用的构造函数,然后在的诺言boostrapModule()得以解决之后。

我不确定在Angular 7中引导程序的行为是否现在有所不同,因为控制台日志提示。以前与Typescript 2一起在Angular 6中使用的解决方案相同,但在版本7中已停止工作。这是基于MSDN文章的已损坏应用程序的堆栈闪电:https ://stackblitz.com/edit/component-inheritance-angular-7

因此,根本的问题是– 如何保证AppInjector.setInjector()之前发生AppInjector.getInjector()

inheritance typescript angular angular7

4
推荐指数
1
解决办法
1368
查看次数

标签 统计

angular ×1

angular7 ×1

inheritance ×1

typescript ×1