Angular 5 中服务的生命周期是多少
What is the lifecycle of a service in Angular 5
Angular 5
服务何时创建和销毁,它的生命周期挂钩是什么(如果有的话)以及它的数据如何在组件之间共享?
编辑:澄清一下,这是不是关于组件生命周期的问题。这个问题是关于服务的生命周期。如果服务没有生命周期,如何管理组件和服务之间的数据流?
服务可以有 2 个作用域。
如果在您的模块上声明了服务,则您拥有相同的实例供所有人共享,这意味着服务将在第一个 component/directive/service/Pipe 需要它的人创建时构建。然后在Module本身销毁的时候销毁(大部分时间是在页面卸载的时候)
如果服务是在Component/Directive/Pipe上声明的,那么每次在Component/Directive/Pipe创建时创建1个实例,在相关的Component/Directive/Pipe销毁时销毁。
代码测试:2 项服务用于在 created/destroyed 时显示。
@NgModule({
providers: [GlobalService] // This means lifeCycle is related to the Module, and only one instance is created for the whole module. It will be created only when the first element who needs it will be created.
})
export class AppModule { }
第二个服务是本地组件服务,将为每个创建的 hello-component
个实例创建,并将在 hello-component
被销毁之前被销毁。
@Injectable()
export class LocalService implements OnDestroy{
constructor() {
console.log('localService is constructed');
}
ngOnDestroy() {
console.log('localService is destroyed');
}
}
@Component({
selector: 'hello',
template: `<h1>Hello {{name}}!</h1>`,
styles: [`h1 { font-family: Lato; }`],
providers: [LocalService]
})
export class HelloComponent implements OnInit, OnDestroy {
@Input() name: string;
constructor(private localService: LocalService, private globalService: GlobalService) {}
ngOnInit(){
console.log('hello component initialized');
}
ngOnDestroy() {
console.log('hello component destroyed');
}
}
如您所见,angular中的Service
可以有OnDestroy
个生命周期钩子。
服务仅存在于其提供者的范围内,因此存在于模块或单个组件的范围内。它们在第一次注入时被实例化,并且只要提供者存在就保持活动状态。
由于服务是正常的类,angulars 生命周期钩子不适用于它们。
OnDestroy
适用于官方文档中提到的服务:https://angular.io/api/core/OnDestroy
引用:
A lifecycle hook that is called when a directive, pipe, or service is destroyed. Use for any custom cleanup that needs to occur when the instance is destroyed.
Angular 5
服务何时创建和销毁,它的生命周期挂钩是什么(如果有的话)以及它的数据如何在组件之间共享?
编辑:澄清一下,这是不是关于组件生命周期的问题。这个问题是关于服务的生命周期。如果服务没有生命周期,如何管理组件和服务之间的数据流?
服务可以有 2 个作用域。
如果在您的模块上声明了服务,则您拥有相同的实例供所有人共享,这意味着服务将在第一个 component/directive/service/Pipe 需要它的人创建时构建。然后在Module本身销毁的时候销毁(大部分时间是在页面卸载的时候)
如果服务是在Component/Directive/Pipe上声明的,那么每次在Component/Directive/Pipe创建时创建1个实例,在相关的Component/Directive/Pipe销毁时销毁。
代码测试:2 项服务用于在 created/destroyed 时显示。
@NgModule({
providers: [GlobalService] // This means lifeCycle is related to the Module, and only one instance is created for the whole module. It will be created only when the first element who needs it will be created.
})
export class AppModule { }
第二个服务是本地组件服务,将为每个创建的 hello-component
个实例创建,并将在 hello-component
被销毁之前被销毁。
@Injectable()
export class LocalService implements OnDestroy{
constructor() {
console.log('localService is constructed');
}
ngOnDestroy() {
console.log('localService is destroyed');
}
}
@Component({
selector: 'hello',
template: `<h1>Hello {{name}}!</h1>`,
styles: [`h1 { font-family: Lato; }`],
providers: [LocalService]
})
export class HelloComponent implements OnInit, OnDestroy {
@Input() name: string;
constructor(private localService: LocalService, private globalService: GlobalService) {}
ngOnInit(){
console.log('hello component initialized');
}
ngOnDestroy() {
console.log('hello component destroyed');
}
}
如您所见,angular中的Service
可以有OnDestroy
个生命周期钩子。
服务仅存在于其提供者的范围内,因此存在于模块或单个组件的范围内。它们在第一次注入时被实例化,并且只要提供者存在就保持活动状态。
由于服务是正常的类,angulars 生命周期钩子不适用于它们。
OnDestroy
适用于官方文档中提到的服务:https://angular.io/api/core/OnDestroy
引用:
A lifecycle hook that is called when a directive, pipe, or service is destroyed. Use for any custom cleanup that needs to occur when the instance is destroyed.