完全免责声明:我对有角度的人非常陌生,例如,我昨天才刚开始使用新的东西。
我有这个自定义模块:
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() {...}
}