Angular 6-如何将配置从`.forRoot()`传递到模块内的服务?

时间:2018-06-26 07:34:34

标签: angular angular-module

完全免责声明:我对有角度的人非常陌生,例如,我昨天才刚开始使用新的东西。

我有这个自定义模块:

foo.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FooComponent } from './foo.component';
import { FooService } from './foo.service';

@NgModule({
  imports: [
    CommonModule
  ],
  exports: [
    FooComponent
  ],
  declarations: [
    FooComponent
  ]
})
@NgModule()
export class FooModule {
  static forRoot(config: any): any {
    return {
      ngModule: FooModule,
      providers: [
        FooService
      ]
    }
  }
}

app.module.ts

app.module的{​​{1}}部分中,我明白了。

imports

我想将... imports: [ BrowserModule, FooModule.forRoot({id: 'SOME_ID'}) ], ... 中的数据传递给.forRoot(args),以便可以对其进行初始化。

初始化FooService后,我想将其注入所需的任何组件中并使用其中的方法。

我的想法是这样的:

foo.service.ts

FooService

foo.component.ts

@Injectable()
export class FooService {
  // get the config from forRoot()
  constructor() {
    // initialize stuff here using
    // the config from forRoot()
  }

  doThis() {...}
  doThat() {...}
}

0 个答案:

没有答案