Questions about angular initialization information?

clipboard.png

three initializations
1. Initialize
2 when defining. Initialize
3 in the constructor What is the difference between initializing
in ngOnInit? Under what circumstances do you usually use which initialization? What is the order in which page rendering is performed in three initializations?


1 is just the syntax sugar of 2, and

is executed in the constructor after compilation.

ngOnInit is executed after constructor, and the component has been constructed when ngOnInit is executed. You can get the properties of the component, such as those entered through @ Input , while the component is still being constructed when constructor is executed, and almost all properties have no value

.

from a development point of view, some fixed value assignments are generally performed in constructor (such as headerSlides); in your figure and some initialization logic in ngOnInit, such as requesting the server to get initialization data

).
Menu