Most common life cycle hooks

From the preceding eight life cycle hooks, you're most likely to use only three of them (in most cases). All three fall into the component type of life cycle hook:

  • ngOnInit: Our initialization logic for our component will go here. You may think that the constructor is the place to add the initialization logic, but ngOnInit is preferable because any binding of data via our interface (that is, input properties) will have been done. This isn't the case with the constructor phase. 
  • ngOnChanges: When we want to know what input properties have changed, and to what they were changed to, this is the place to look.
  • ngOnDestroy: This is where we insert our cleanup logic for our component (if we have any things we'd like to clean up—otherwise, we don't use it).

Here is an example of how we hook into the ngOnInit life cycle hook (we'll just write out some output to our console):

class CarComponent {
ngOnInit() {
console.log('An instance of our CarComponent has
been fully initialized.');
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.63.87