AfterViewInit

一个生命周期钩子,会在 Angular 完全初始化了组件的视图后调用。 定义一个 ngAfterViewInit() 方法来处理一些额外的初始化任务。

A lifecycle hook that is called after Angular has fully initialized a component's view. Define an ngAfterViewInit() method to handle any additional initialization tasks.

      
      interface AfterViewInit {
  ngAfterViewInit(): void
}
    

实现类

参见

方法

一个回调方法,它会在 Angular 完成了组件视图的初始化逻辑之后立即调用。 在视图初始化完成之后,它只会调用一次。

A callback method that is invoked immediately after Angular has completed initialization of a component's view. It is invoked only once when the view is instantiated.

ngAfterViewInit(): void
      
      ngAfterViewInit(): void
    
参数

没有参数。

返回值

void

使用说明

下列代码片段展示了组件如何实现该接口,以定义自己的视图初始化逻辑。

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

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