是否可以为使用@Injectable()
注释的服务设置生命周期挂钩
我本以为生命周期钩子会在这样的服务上调用,但事实证明我错了,它似乎只在@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 }
}
https://stackoverflow.com/questions/36188966
复制相似问题