简体   繁体   中英

How can an angular module use another module and instantiate it with custom value?

I need to have a module that does instantiate another one with a custom variable like the following.

// app.module.ts
MyServiceModule.forRoot({
 custom: 'customVar'
})

Then within the myServiceModule I try to do the following

#NgModule({
  imports: [
    anotherServiceModule.forRoot({
      custom: // <-- The 'customVar' from MyServiceModule
    }) 
  ]
})
export class MyServiceModule {
  static forRoot(config: {custom: string}) {
    return {
      ngModule: MyServiceModule,
      providers: [
        {
           provide: MyServiceProvider,
           useValue: config,
        }
      ]
    }
  }
}

Can I somehow use the instance of my MyServiceProvider ?

The services within AnotherServiceModule could require MyServiceProvider as a dependency. I'm guessing that by setting it's useValue in MyServiceModule.forRoot it would then be available as that value in the other services

I'm assuming AnotherServiceModule is something you have control over, and isn't a 3rd party module

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM