填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫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
}
    

參見

方法

一個用於執行清理邏輯的回呼(Callback)方法,會在指令、管道、服務的實例被銷燬後立即呼叫。

A callback method that performs custom clean-up, invoked immediately before 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() {
    // ...
  }
}