Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Angular have browser-specific life cycle hooks like React?

For instance React's componentWillMount():

This is the only lifecycle hook called on server rendering. Generally, we recommend using the constructor() instead.

And in React's componentDidMount()

componentDidMount() is invoked immediately after a component is mounted. Initialization that requires DOM nodes should go here.

However I do not see anything in the Angular doc specifying which ones are executed on the server and which ones are executed on the browser (or both). Or should I assume that all of them are executed on both? If so, how do you do things that require the DOM (window, document, etc.)?

like image 564
David Barratt Avatar asked Jul 06 '26 11:07

David Barratt


1 Answers

No, it doesn't. It runs the same hooks.

There's plenty of devices you can use to avoid contact with plain DOM, like HostBinding and HostListener decorators, Renderer and ElementRef classes. Remember that Angular 2 is a high-abstraction tool. It doesn't promotes access the DOM, although it is possible. Also, for server-side rendering, DI also comes to the rescue when there are different implementations for server and client.

Learn about Angular Universal to know more about server-side rendering at https://github.com/angular/universal.

If that doesn't answer your question, please place a more specific question.

like image 97
André Werlang Avatar answered Jul 09 '26 03:07

André Werlang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!