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
Angular compiler - custom decorators with ngOnDestroy does not work with AOT build #16023
changed the title [-]Custom decorators does not work with AOT build[/-][+]Angular compiler - custom decorators does not work with AOT build[/+]on Apr 15, 2017
When working with JIT mode, everything is working fine.
When working with AOT - only when I add the ngOnDestroyexplicitly, Angular will call my decorated method, but in JIT mode it's working even if I don't define this method explicitly.
@Component({
...
})
@Test()exportclassTodoComponent{// When I am in AOT mode, and I don't define this method my decorated method will not run ngOnDestroy(){}}
Any ideas why this is happening?
Reacted by Roberto Simonetti, Juan Pinilla, Allen and Benjamin
changed the title [-]Angular compiler - custom decorators does not work with AOT build[/-][+]Angular compiler - custom decorators with ngOnDestroy does not work with AOT build[/+]on Apr 18, 2017
The problem doesn't seem to relate only to lifecycle hooks methods: AoT mode doesn't work also if you try to create a custom property or a custom method into the decorator.
This is a general "problem" with AOT: It relies on being able to statically analyze things like lifecycle hooks, properties with @Input on them, constructor arguments, ...
As soon as there is dynamic logic that adds new methods / properties, our AOT compiler does not know about this.
As we are moving to AOT by default in the future, this won't go away.
Sorry, but closing as infeasible.
Reacted by Lazar Ljubenović
Reacted by Oliver Janik, Artem Andreev, Brecht Billiet, Olga Grabek, Zach Henwood, Mark Park, Jian Zhou, nasreddine skandrani, pedep, Denis Itskovich and 5 more
This is already supported in Ivy, no need for any more idea.
Life-cycle hooks are always checked in runtime in Ivy, but when using decorator it’s not even available at runtime due to evaluation timing. Can only be done in the current (new) decorator proposal, not the deprecated (TypeScript’s) one.
(By the way, also not work in JIT now if decorator order is wrong for ivy’s checking)
@nasreddineskandrani I don’t know what you mean delayed, that’s a different proposal which being much easier to implement in Ivy, the design and work should only be started after Ivy becoming stable.
@trotyl
the first proposal was not on ivy and they closed it #20682
The bad news is that we don't want to add it to ViewEngine and than have to reimplement in Ivy because the API will be different in Ivy. So for now I am going to close this PR and we will add this feature to Ivy.
I didn't try out ivy yet. So when you said it's in ivy i thought they did it already.
@nasreddineskandrani It's closed because not implementable with ViewEngine's structure, it can never be complete even without being closed. It's much more possible in Ivy but still not that easy.
I'm working on a library called NgObservable that implements custom lifecycle decorators that are AoT cross-compatible with ViewEngine and Ivy. The tradeoff is that each component must extend the NgObservable base class to get around the AoT limitations, which in turn implements every angular lifecycle hook leading to slightly larger component factories.
It's still in the works but I'm keen to get feedback. You can grab it on NPM.
This is not a fix to the actual issue but it's a solution we found to prevent us from leaving decorators that depend on ngOnDestroy without the hook. We make sure the target of the decorator has the OnDestroy type, that way compiling will error if you don't implement OnDestroy.
example:
function SomeDecorator(target: OnDestroy) {
...
}
if you use it without correctly implement OnDestroy you'll see something like error TS2345: Argument of type 'SomeComponent' is not assignable to parameter of type 'OnDestroy'. With that type validation, it won't let you forget about the ngOnDestroy hook by mistake.
If your decorator adds properties to the target you can use a custom type that extends OnDestroy
Activity
[-]Custom decorators does not work with AOT build[/-][+]Angular compiler - custom decorators does not work with AOT build[/+]tbosch commentedon Apr 17, 2017
Hello,
could you create a reproduction for this, e.g. a branch on Github?
ArielGueta commentedon Apr 17, 2017
After investigating, I found the real problem, and it's interesting.
In my custom decorator I'm trying to override the
ngOnDestroymethod, for example:But -
When working with JIT mode, everything is working fine.
When working with AOT - only when I add the
ngOnDestroyexplicitly, Angular will call my decorated method, but in JIT mode it's working even if I don't define this method explicitly.Any ideas why this is happening?
[-]Angular compiler - custom decorators does not work with AOT build[/-][+]Angular compiler - custom decorators with ngOnDestroy does not work with AOT build[/+]robisim74 commentedon Apr 30, 2017
Same problem here using ngOnInit: I'm creating a class decorator that uses
applyto modify the ngOnInit method:It works fine in JiT mode, but in AoT it works only if ngOnInit method already exists in the component.
robisim74 commentedon May 1, 2017
@tbosch I created a repository: https://github.com/robisim74/angular-decorators
JiT mode:
npm startAoT mode:
npm run buildThe problem doesn't seem to relate only to lifecycle hooks methods: AoT mode doesn't work also if you try to create a custom property or a custom method into the decorator.
NetanelBasal commentedon May 1, 2017
Hey @tbosch ,
The same problem is here.
https://github.com/NetanelBasal/ngx-auto-unsubscribe
https://github.com/NetanelBasal/angular2-take-until-destroy
Both modify the
ngOnDestroyhook.robisim74 commentedon May 9, 2017
@tbosch @vicb Any updates about this issue? Is it a bug? Or are there other solutions? Thanks.
tbosch commentedon May 10, 2017
This is a general "problem" with AOT: It relies on being able to statically analyze things like lifecycle hooks, properties with
@Inputon them, constructor arguments, ...As soon as there is dynamic logic that adds new methods / properties, our AOT compiler does not know about this.
tbosch commentedon May 10, 2017
As we are moving to AOT by default in the future, this won't go away.
Sorry, but closing as infeasible.
18 remaining items
trotyl commentedon Sep 29, 2018
This is already supported in Ivy, no need for any more idea.Life-cycle hooks are always checked in runtime in Ivy, but when using decorator it’s not even available at runtime due to evaluation timing. Can only be done in the current (new) decorator proposal, not the deprecated (TypeScript’s) one.
(By the way, also not work in JIT now if decorator order is wrong for ivy’s checking)
nasreddineskandrani commentedon Sep 29, 2018
@trotyl
Yep that's what i wrote here #20941
for now it's delayed, no? o O or i missed something (exclusive to ivy)
trotyl commentedon Sep 29, 2018
@nasreddineskandrani I don’t know what you mean delayed, that’s a different proposal which being much easier to implement in Ivy, the design and work should only be started after Ivy becoming stable.
nasreddineskandrani commentedon Sep 29, 2018
@trotyl
the first proposal was not on ivy and they closed it #20682
I didn't try out ivy yet. So when you said it's in ivy i thought they did it already.
trotyl commentedon Sep 29, 2018
@nasreddineskandrani It's closed because not implementable with ViewEngine's structure, it can never be complete even without being closed. It's much more possible in Ivy but still not that easy.
alshdavid commentedon Feb 15, 2019
Why not allow us to use comments to tell the static analysis tool to add certain hooks to classes if not already present?
Say we decorate the decorator
dinohorvat commentedon Mar 27, 2019
Are there any plans for this? Or it will be ignored completely.
stupidawesome commentedon May 26, 2019
I'm working on a library called NgObservable that implements custom lifecycle decorators that are AoT cross-compatible with ViewEngine and Ivy. The tradeoff is that each component must extend the
NgObservablebase class to get around the AoT limitations, which in turn implements every angular lifecycle hook leading to slightly larger component factories.It's still in the works but I'm keen to get feedback. You can grab it on NPM.
therepo90 commentedon Jul 9, 2019
Lack of custom decorators is one of the most annoying things in angular...
There is "jit" flag in module thought that could help but that's not cool.
haskelcurry commentedon Aug 17, 2019
Any custom decorators don't work with AOT, not only method decorators.
And yes, I agree with @therepo90 that it's very annoying.
jupapios commentedon Aug 23, 2019
This is not a fix to the actual issue but it's a solution we found to prevent us from leaving decorators that depend on
ngOnDestroywithout the hook. We make sure the target of the decorator has theOnDestroytype, that way compiling will error if you don't implementOnDestroy.example:
if you use it without correctly implement
OnDestroyyou'll see something likeerror TS2345: Argument of type 'SomeComponent' is not assignable to parameter of type 'OnDestroy'.With that type validation, it won't let you forget about thengOnDestroyhook by mistake.If your decorator adds properties to the target you can use a custom type that extends
OnDestroyCole-Beckwith-SP commentedon Sep 16, 2019
The existing workaround sounds like it's adding an empty lifecycle hook to the component. Will this continue to work while using Ivy?
robisim74 commentedon Sep 17, 2019
@Cole-Beckwith-SP See here: #31495
angular-automatic-lock-bot commentedon Oct 18, 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.