You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug(Lifecycle): Lifecycle hooks not called unless defined on the prototype #7270
The lifecycle hooks (component, router) are called if defined on the class, or the function prototype, but are not called if defined in the constructor.
classMyComponentimplementsOnInit{ngOnInit(){// works}}classMyComponentimplementsOnInit{ngOnInit: any=()=>{// no TypeScript warning, never called}}functionMyComponent(){}MyComponent.prototype.ngOnInit=function(){// works};functionMyComponent(){this.ngOnInit=function(){// never called};}
I expect the lifecycle hook to be called in either scenario. Is this by design?
Easy to guess why as it would be safer to analyze a prototype and store the information about the component rather than instantiate one (risk of it doing something in the ctor) to find out what it has.
What use cases argue for this proposal? Isn't it fairly easy to workaround?
Sorry, this is working as intended. The reason for this is that we need to code-gen different code depending if we have such a hook or not. Code-gen can only access the prototype chains, as we can't instantiate/run code. So if you want speed, prototype is the only thing we can look at.
Activity
ValeriiVasin commentedon Mar 2, 2016
Have the same issues with lifecycle hooks. That was totally unexpected...
Will it be fixed?
BTW, React lifecycle hooks behave the expected way.
wardbell commentedon Mar 23, 2016
Easy to guess why as it would be safer to analyze a prototype and store the information about the component rather than instantiate one (risk of it doing something in the ctor) to find out what it has.
What use cases argue for this proposal? Isn't it fairly easy to workaround?
mhevery commentedon Mar 25, 2016
Sorry, this is working as intended. The reason for this is that we need to code-gen different code depending if we have such a hook or not. Code-gen can only access the prototype chains, as we can't instantiate/run code. So if you want speed, prototype is the only thing we can look at.
angular-automatic-lock-bot commentedon Sep 8, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.