@Injectable()
という注釈が付いたサービスにライフサイクルフックを付けることは可能ですか?angular2のサービスのライフサイクルメソッド
私はこのようなサービスでライフサイクルフックを呼び出すと予想していましたが、私は間違っていると証明されました。それは@Component
でしか動作していないようです。依存関係注入がサービスを作成/破壊するときに、サービスに情報を得る方法はありますか?
import {Component, Injectable, OnInit, OnDestroy} from 'angular2/core';
@Injectable()
export class SampleService implements OnInit, OnDestroy {
ngOnInit() {
console.log("OnInit")
}
ngOnDestroy() {
console.log("OnDestroy")
}
}
@Component({
selector: "sample",
template: "<div>Sample Component</div>",
providers: [ SampleService ]
})
export class SampleComponent {
constructor() { private _sampleService: SampleService }
}
これは他の質問の重複ではありません。 OnDestroyのドキュメンテーションは実際にはサービスがそれをサポートすべきだと述べています。私も間違っていることが判明した。 –