简体   繁体   中英

Do we need afterViewInit lifecycle hook anymore in angular?

We know that the afterViewInit lifecycle hook gets triggered once when a component finally finishes the initialization of it's view. Thus if any component has to work with it's viewChild, this lifecycle hook get's it's relevance to be invoked inside the component.

But these days we don't have to depend on afterViewInit lifecycle as we can configure the viewchild with setting the "static" property to "true" to tell angular that we want to resolve the query result before change detection (if this was the case).

My question is, if this was the only purpose of ngAfterViewInit to write the logic that depends on component's viewchild, do we really need to use this lifecycle hook anymore if we can work the other way around?

You can't always use static: true sometimes the visibility of the viewChild or the child component is dynamic means that it does change, for instance viewChild element where the visibility is based on a directive like * ngIf . you can't use static: true in that case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM