0
私はいくつかのサービスがAngular 4/5 autoinstaceサービスプロバイダで可能ですか?
// app.module
@NgModule({
providers: [
AuthorsService
],
bootstrap: [
AppComponent
]
})
// authors.service
@Injectable()
export class AuthorsService {
public constructor() {
console.log('service constructor');
}
}
前のコードのようなprovices配列に追加されたときに、私は、実行console.log
のためのサービスで何かを行うことができますように宣言していますか?
サービス開始私はサービスインスタンスのために、このようなコードを避けるために、好き...いくつかのものを行う必要が:
public constructor(
protected authorsService: AuthorsService
) {
}
// only with that console.log is runned
(、なぜ聞かないでください、これは単純な例
for a better use on a libraryです)
返信用の返信(「APP_INITIALIZER」の例:https://github.com/angular/angular/issues/9047#issuecomment-224075188)。しかし私は奉仕の面でも必要です:/。私はどんな解決策も見つけることはできません、あなたの解決策は最高になることができます。 – pablorsk
サービスに依存関係がなく、(質問に示されているように)同期して初期化されている場合は、モジュールコンストラクタにも注入できます。 – estus