Suppose I have three components: P1, C1, and C2.
When constructor or Child C1 get called with super we call the constructor of parent P1 component similarly for C2 child, now I want to know from which child (C1/C2) the parent constructor get called.
How to get the name or instance of the child component in patent component in angular?
Parent
class can be inherited by componentsC1
andC2
by using the extends keyword.When this is done the properties and methods are inherited by the child.
When two classes/components inherit a single class
P1
Then that means there are two separate scopes ofP1
inherited by the children, each component has its own set ofproperties/methods
from the parent and changes are both completely isolated from each other.During inheritance, its mandatory to call the
super()
inside the constructor of the children. This is required to call the constructor of the parent. In angular, you can use this super, to pass thedependency injection
properties likerouter
,activatedRoute
to the parent, so that the parent methods have access to them.This is a great method for
core reusability/sharing
between multiple components!Method 1
Parent
Child B
Child C
Method 2
Parent
Children can do