Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement OnInit and OnChanges on same component with type safety

One of the things I like about typescript is its type safety and I use it consistently.

Typescript does not support multiple inheritance, so how can I extend an Angular 2 component class by OnInit and OnChanges at the same time?

I know that it works without declaring the inheritance but I like my code without squiggly lines.

like image 930
hholtij Avatar asked Jul 04 '16 13:07

hholtij


1 Answers

OnInit and OnChanges are not classes but interfaces, so your components don't inherit but implement those interfaces.

As a class can implements as many interfaces as it needs, there is no problem implementing both OnInit and OnChanges interfaces on the same component.

For example:

class FooComponent implements OnInit, OnChanges {
}

For more information, have a look at Angular Lifecycle Hooks guide.

like image 74
Blackus Avatar answered Sep 27 '22 23:09

Blackus