What's the difference between ngOnInit and ngAfterViewInit of Angular2?

前端 未结 3 597
失恋的感觉
失恋的感觉 2021-01-30 09:55

I can not understand what the difference between ngOnInit and ngAfterViewInit.

I found the only difference between them is @ViewChild

3条回答
  •  一整个雨季
    2021-01-30 10:33

    ngOnInit() is called right after the directive's data-bound properties have been checked for the first time, and before any of its children have been checked. It is invoked only once when the directive is instantiated.

    ngAfterViewInit() is called after a component's view, and its children's views, are created. Its a lifecycle hook that is called after a component's view has been fully initialized.

提交回复
热议问题