填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写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 }
      
      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
      
      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() { // ... } }
      
      @Component({selector: 'my-cmp', template: `...`})
class MyComponent implements OnInit {
  ngOnInit() {
    // ...
  }
}