填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

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.

      
      interface OnDestroy {
  ngOnDestroy(): void
}
    

参见

方法

一个用于执行清理逻辑的回调方法,会在指令、管道、服务的实例被销毁后立即调用。

A callback method that performs custom clean-up, invoked immediately before a directive, pipe, or service instance is destroyed.

ngOnDestroy(): void
      
      ngOnDestroy(): void
    
参数

没有参数。

返回值

void

使用说明

下列代码片段展示了组件如何实现该接口,以定义它自己的清理逻辑。

The following snippet shows how a component can implement this interface to define its own custom clean-up method.

@Component({selector: 'my-cmp', template: `...`}) class MyComponent implements OnDestroy { ngOnDestroy() { // ... } }
      
      @Component({selector: 'my-cmp', template: `...`})
class MyComponent implements OnDestroy {
  ngOnDestroy() {
    // ...
  }
}