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 after a directive, pipe, or service instance is destroyed.

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

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() { // ... } }