填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

OnInit

一個生命週期鉤子,它會在 Angular 初始化完了該指令的所有資料繫結屬性之後呼叫。 定義 ngOnInit() 方法可以處理所有附加的初始化任務。

A lifecycle hook that is called after Angular has initialized all data-bound properties of a directive. Define an ngOnInit() method to handle any additional initialization tasks.

      
      interface OnInit {
  ngOnInit(): void
}
    

參見

方法

它的呼叫時機在預設的變更檢測器首次檢查完該指令的所有資料繫結屬性之後,任何子檢視或投影內容檢查完之前。 它會且只會在指令初始化時呼叫一次。

A callback method that is invoked immediately after the default change detector has checked the directive's data-bound properties for the first time, and before any of the view or content children have been checked. It is invoked only once when the directive is instantiated.

      
      ngOnInit(): void
    
引數

沒有引數。

返回值

void

使用說明

下列片段展示了元件要如何實現此介面,以定義它自己的初始化方法。

The following snippet shows how a component can implement this interface to define its own initialization method.

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