how the parameters of a forRoot() module's method is passed to a provider?

前端 未结 1 1182
别那么骄傲
别那么骄傲 2021-01-15 18:24

I found some examples of module\'s forRoot() method like the one below:

export class CoreModule {
constructor(
@Optional()
@SkipSelf()
parentModule: CoreModu         


        
相关标签:
1条回答
  • 2021-01-15 18:56

    Use InjectionToken to register the parameters with the injector. Then use DI passing in the InjectionToken with the deps property as follows:

    export const Params= new InjectionToken<string[]>('params');
    
    ...
    
    static forRoot(someParameters?:string[]): ModuleWithProviders {
      return {
      ngModule: CoreModule,
      providers: [
                { provide: Params, useValue: someParameters },
                { provide: AnProvider1, useClass: AnProvider1, deps:[Params] },
                AnProvider2
      ]
    };
    

    In your component constructor, use the InjectionToken:

    constructor(@Inject(Params) someParameters: string[])
    
    0 讨论(0)
提交回复
热议问题