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
Allow custom decorators that use lifecycle hooks in AoT - these are our version of hooks, but we can't use them #30497
When writing a custom decorator, lifecycle methods you patch are removed in AoT because the compiler doesn't check that custom decorators are using them.
This results in an inability to create custom decorators with any meaningful component specific behaviour.
React uses hooks to help patch into setup/teardown of your functional components, decorators are Angulars form of hooks. It would be super useful to have this functionality in Angular.
Reacted by Paul Sherwood, bryanrideshark, Martin Koppehel, Cory Rylan and Максим Иванов
It looks like the onChanges life cycle works, but ngOnInit doesn't. Using a production build running with AoT, in the console I can see required and on changes, but not init. Switching to JIT development I correctly see the init messages.
If it's hard for the compiler to statically analyse a custom decorator to determine what lifecycle methods are being patched, perhaps a basic solution like using comments which target the compiler will be a simple temporary solution.
In Ivy, lifecycle hooks are purely a runtime concern. They're read off the prototype of the component as soon as the first instance is created, which means that if a decorator adds hooks to the prototype then Ivy will notice them.
@alxhub Should this be working in 8.0? I enabled Ivy and tried adding hooks with decorators using renderComponent() and platformBrowserDynamic().bootstrapModule() but it still doesn't work in AoT without adding an empty stub to the class definition.
They're read off the prototype of the component as soon as the first instance is created
This is not true, lifecycle hooks are determined in defineComponent(), which happen when the constructor is created, not the instance.
lifecycle hooks are purely a runtime concern.
However, decorators* are also purely a runtime concern, as explained in #16023 (comment), decorators runs after static properties, can be reproduce easily in TypeScript playground.
When defineComponent() get called, all the decorators hasn't being executed yet, so no methods can be found in the prototype.
The current decorator proposal has changed to compile-time semantics, but what implemented in TypeScript is still the obsolete stage-0 proposal.
Activity
christopher-kiss commentedon May 16, 2019
@alshdavid,
It looks like the onChanges life cycle works, but ngOnInit doesn't. Using a production build running with AoT, in the console I can see
requiredandon changes, but notinit. Switching to JIT development I correctly see theinitmessages.Would be great to have this fixed.
Edit -
As a workaround, I've had to resort to creating a class that implements these and extend this class.
alshdavid commentedon May 16, 2019
If it's hard for the compiler to statically analyse a custom decorator to determine what lifecycle methods are being patched, perhaps a basic solution like using comments which target the compiler will be a simple temporary solution.
eg
trotyl commentedon May 16, 2019
Duplicate of #16023
alxhub commentedon May 28, 2019
I have some good news!
In Ivy, lifecycle hooks are purely a runtime concern. They're read off the prototype of the component as soon as the first instance is created, which means that if a decorator adds hooks to the prototype then Ivy will notice them.
stupidawesome commentedon May 29, 2019
@alxhub Should this be working in 8.0? I enabled Ivy and tried adding hooks with decorators using
renderComponent()andplatformBrowserDynamic().bootstrapModule()but it still doesn't work in AoT without adding an empty stub to the class definition.Should this code theoretically work?
trotyl commentedon May 29, 2019
@alxhub
This is not true, lifecycle hooks are determined in
defineComponent(), which happen when the constructor is created, not the instance.However, decorators* are also purely a runtime concern, as explained in #16023 (comment), decorators runs after static properties, can be reproduce easily in TypeScript playground.
When
defineComponent()get called, all the decorators hasn't being executed yet, so no methods can be found in theprototype.The current decorator proposal has changed to compile-time semantics, but what implemented in TypeScript is still the obsolete stage-0 proposal.
christopher-kiss commentedon May 29, 2019
@trotyl,
So it looks like this is blocked until Typescript updates the decorator implementation? Which doesn't look like it's in a release plan yet: https://github.com/Microsoft/TypeScript/wiki/Roadmap#future
12 remaining items
fix(core): Allow modification of lifecycle hooks any time before boot…
angular-automatic-lock-bot commentedon Aug 15, 2020
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.
fix(core): Allow modification of lifecycle hooks any time before boot…