可配置的角度9模块

时间:2020-03-15 12:39:56

标签: angular typescript dependency-injection module

我想创建具有IVY和AOT的可配置Angular 9模块。

在当前版本的Angular中,默认情况下

IVY和AOT已打开:

npx @angular/cli@9.0.6 new ng-modules --style=scss --routing=false

在最简单的情况下,它必须提供可通过名称配置的单个有状态服务:

counter.module.ts

@Injectable()
export class CounterService {
  private counter = 0;
  shot() {
    return this.counter++;
  }
}

@NgModule()
export class CounterModule {
  static withConfig(name: string): ModuleWithProviders {
    return {
      ngModule: CounterModule,
      providers: [{
        provide: name,
        useClass: CounterService
      }]
    };
  }
}

app.module.ts

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    CounterModule.withConfig('main.CountService'),
    CounterModule.withConfig('integration.CountService'),
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'ng-modules-shots';
  constructor(
    @Inject('main.CountService') counter: CounterService,
  ) {
    this.title = '' + counter.shot();
  }

}

此时一切正常。但是,如果我想向CounterModule.withConfig中添加任何逻辑:

counter.module.ts

...
@NgModule()
export class CounterModule {
  static withConfig(name?: string): ModuleWithProviders {
    const counterProviderToken = name ? name : CounterService;
    return {
      ngModule: CounterModule,
      providers: [{
        provide: counterProviderToken,
        useClass: CounterService
      }]
    };
  }
}

我遇到错误:

ERROR in src/app/app.module.ts:11:16 - error NG1010: Value at position 1 in the NgModule.imports of AppModule is not a reference: [object Object]

我该怎么办?修复它吗?也许还有另一种方法来制作可配置模块?

1 个答案:

答案 0 :(得分:1)

当前存在一个约束,即该函数中只有一个语句。因此,如果要向其中添加任何逻辑并保持AOT正常工作,则应将其放在此语句中。

...
@NgModule()
export class CounterModule {
  static withConfig(name?: string): ModuleWithProviders {
    return {
      ngModule: CounterModule,
      providers: [{
        provide: name ? name : CounterService,
        useClass: CounterService
      }]
    };
  }
}

不过,您可以在此模块中使用函数来生成提供程序:

...
@NgModule()
export class CounterModule {
  static withConfig(name?: string): ModuleWithProviders {
    return {
      ngModule: CounterModule,
      providers: this.generateProviders(name)
    };
  }

  private static generateProviders(name?: string): Provider[] {
    const counterProviderToken = name ? name : CounterService;
    return [{
      provide: counterProviderToken,
      useClass: CounterService
    }]
  }
}