What is the advantage of implementing the Angular lifecycle event interfaces?

Asked

Viewed 64 times

8

Angular has an interface for each event of its life cycle, for example, OnInit for the method ngOnInit

Since it is not necessary to implement such interfaces in order to benefit from the events, what is the advantage of implementing the interfaces of Angular lifecycle events in the components that use these events?

1 answer

9


Use of the lifecycle angular interfaces is optional. They only help you as a Veloper. Technically, Typescript is converted to Javascript, with the transpile process, which has no interfaces. Simply call the Lifecycle Javascript methods if they exist. That’s why it makes no difference whether you use the interfaces or not. However, you should use the interfaces for several reasons:

  • It is clearer which "life cicle" events are actually used. In large classes with many methods, you quickly lose sight general. The use of interfaces makes it possible to quickly determine all "life cicle" methods used in a single location - at the beginning of the class in Typescript.
  • The Typescript compiler warns if you do not implement "life cicle" correctly, for example, if you forgot to implement the method, incorrectly typed the method name or the method was accidentally removed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.