AoT compiled components do not call inherited lifecycle hooks
See original GitHub issueI’m submitting a … (check one with “x”)
[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior The AoT compiler does not look up the inheritance tree when checking for lifecycle hooks, which results in different behaviour than when compiling JiT.
Expected behavior
The AoT compiler should search all super classes of a component for implementation of lifecycle hooks.
Minimal reproduction of the problem with instructions
class BaseComponent implements OnInit {
ngOnInit() {
// do something important
}
}
@Component( ... )
class ConcreteComponent extends BaseComponent {
...
}
When running in JiT mode, the ngOnInit() method will be called when the ConcreteComponent is instantiated. But in AoT mode, the function will not be called.
Currently, the workaround is to explicitly implement all the super class hooks:
@Component( ... )
class ConcreteComponent extends BaseComponent {
ngOnInit() {
super.ngOnInit();
}
}
This is the case for all the lifecycle hooks, not just ngOnInit.
What is the motivation / use case for changing the behavior?
This is especially important for the @angular/upgrade/static library which has a base class, UpgradeComponent, for Angular 2 components that are used to upgrade Angular 1 components.
- Angular version: <=2.2.0 (and master at time of writing)
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:10 (4 by maintainers)

Top Related StackOverflow Question
This is still an issue (tested in
4.3.6and4.1.1), I think this issue should be reopened unless it has been fixed in the 5 build?Edit for those seeing this in the future: it seems to have been fixed from Angular 5.
@ohjames I’m pretty sure. The app works only with
or building without
--prod