在Angular 5中,服务的生命周期是什么

在Angular 5中,服务的生命周期是什么,angular,lifecycle,angular-services,Angular,Lifecycle,Angular Services,角度5 服务是在什么时候创建和销毁的,它的生命周期挂钩是什么(如果有的话),它的数据是如何在组件之间共享的 编辑:澄清一下,这不是关于组件生命周期的问题。这个问题与服务的生命周期有关。如果服务没有生命周期,那么如何管理组件和服务之间的数据流?服务可以有两个作用域 如果在您的模块上声明了服务,则所有模块共享相同的实例,这意味着将在创建第一个需要它的组件/指令/服务/管道时构造该服务。然后,当模块本身被销毁时,它将被销毁(大多数情况下,当页面被卸载时) 如果服务是在组件/指令/管道上声明的,那么每次

角度5

服务是在什么时候创建和销毁的,它的生命周期挂钩是什么(如果有的话),它的数据是如何在组件之间共享的


编辑:澄清一下,这不是关于组件生命周期的问题。这个问题与服务的生命周期有关。如果服务没有生命周期,那么如何管理组件和服务之间的数据流?

服务可以有两个作用域

如果在您的模块上声明了服务,则所有模块共享相同的实例,这意味着将在创建第一个需要它的组件/指令/服务/管道时构造该服务。然后,当模块本身被销毁时,它将被销毁(大多数情况下,当页面被卸载时)

如果服务是在组件/指令/管道上声明的,那么每次创建组件/指令/管道时将创建一个实例,并在销毁相关组件/指令/管道时销毁一个实例

@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 { }
@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');
  }
}

代码测试:2个服务用于显示它们的创建/销毁时间

@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 { }
@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');
  }
}
第二个服务是本地组件服务,将为创建的每个
hello组件
实例创建,并将在销毁
hello组件
之前销毁

@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 { }
@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');
  }
}
@Injectable()
导出类LocalService实现OnDestroy{
构造函数(){
log('localService已构造');
}
恩贡德斯特罗(){
log('localService已销毁');
}
}
@组成部分({
选择器:“你好”,
模板:`Hello{{name}}!`,
样式:[`h1{font-family:Lato;}`],
提供者:[本地服务]
})
导出类HelloComponent实现OnInit、OnDestroy{
@Input()名称:string;
构造函数(私有localService:localService,私有globalService:globalService){}
恩戈尼尼特(){
log('hello component initialized');
}
恩贡德斯特罗(){
log(“hello组件已销毁”);
}
}

如您所见,angular中的
服务可以具有
OnDestroy
生命周期挂钩。

服务仅在其提供者的范围内,因此在模块或单个组件的范围内。它们在第一次注入时被实例化,并在提供者存在时保持活动状态


由于服务是普通类,angulars生命周期挂钩不适用于它们

OnDestroy
适用于官方文件中所述的服务:

引述:

当指令、管道或服务被销毁时调用的生命周期挂钩。用于销毁实例时需要进行的任何自定义清理

@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 { }
@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');
  }
}

阅读此@ochs.tobi,您的评论与组件生命周期有关。我问的是服务问题lifecycles@HiteshKansagara该链接涉及组件的生命周期。我问的是服务问题lifecycles@RahulSaha我相信这些服务是在app.module加载时创建的检查我的答案,正如您所看到的,我们可以使用
OnDestroy
lifecycle钩子。在:root中声明为Provided的服务如何?我相信它不需要在ngModule中声明。@foo-baar它将如您所期望的那样工作,我们只是采取这种方式,因为我相信声明它的范围不是服务的责任。这完全是主要的基本观点,在一天结束时,这两个都是重要的通知-模块永远不会被销毁,除非手动这样做;这意味着它的服务也不会被破坏!要解决这个问题,您必须首先就这个问题投票