Angular 2.3 is released! One of the exciting new feature is component inheritance. Component inheritance is very powerful and it can increase your code reusability.
What does component inheritance provide us?
Component Inheritance in Angular 2.3 covers all of the following:
Metadata (decorators): metadata (e.g.
@Output), etc. defined in a derived class will override any previous metadata in the inheritance chain otherwise the base class metadata will be used.
Constructor: the base class constructor will be used if the derived class doesn’t have one, this mean that all the services you injected in parent constructor will be inherited to child component as well.
Lifecycle hooks: parent lifecycle hooks (e.g.
ngOnChanges) will be called even when are not defined in the derived class.
Component inheritance DO NOT cover templates and styles. Any shared DOM or behaviours must be handled separately.
Let's start with a simple use case.Essential Reading: Learn React from Scratch! (2019 Edition)
Someone wrote a pagination component. You like the component and its logic. The functionality is exactly what you are looking for. You want to use it, but you want to apply your own styles.
Here's a quick look at what we'll be building.