Repository navigation
Closed
Labels
area: coreIssues related to the framework runtimeIssues related to the framework runtimecore: lifecycle hooksfreq1: lowregressionIndicates than the issue relates to something that worked in a previous versionIndicates than the issue relates to something that worked in a previous versionstate: confirmedtype: bug/fix
Milestone
Description
🐞 bug report
Affected Package
The issue is caused by package @angular/compiler-cli (I guess)Is this a regression?
I had never tried AoT with IvyDescription
I have a custom property decorator like this:export function Language(): PropertyDecorator {
function DecoratorFactory(target: any): void {
const targetNgOnInit: () => void = target.ngOnInit;
function ngOnInit(this: any): void {
// Just for example
console.log('onInit');
if (targetNgOnInit) {
targetNgOnInit.apply(this);
}
}
target.ngOnInit = ngOnInit;
}
return DecoratorFactory;
}
used as:
@Language() lang: string;
ngOnInit() {
// For aot
}
- With AoT compiler, it works (it writes onInit in console)
- If I enable Ivy and disable AoT, it works
- If I enable Ivy and AoT, it doesn't work
🔬 Minimal Reproduction
🔥 Exception or Error
No errors during compilation or in console. The ngOnInit method in decorator function is ignored.
🌍 Your Environment
Angular Version:
Angular CLI: 8.1.1
Node: 10.14.2
OS: win32 x64
Angular: 8.1.1
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.801.1
@angular-devkit/build-angular 0.801.1
@angular-devkit/build-optimizer 0.801.1
@angular-devkit/build-webpack 0.801.1
@angular-devkit/core 8.1.1
@angular-devkit/schematics 8.1.1
@ngtools/webpack 8.1.1
@schematics/angular 8.1.1
@schematics/update 0.801.1
rxjs 6.4.0
typescript 3.4.5
webpack 4.35.2
Anything else relevant?
Tried without success also the next version of Angular.
Thanks
Reacted by jase, Hu Zheng, Ilya Borisov, Nikita Balakirev, Manuel, Gregor Woiwode, Максим Иванов, Simon Bobrov, Daniel Sharkov, Vasiliy Ufimtsev and 22 more
Reacted by Klemen Oslaj and G
Metadata
Metadata
Assignees
Labels
area: coreIssues related to the framework runtimeIssues related to the framework runtimecore: lifecycle hooksfreq1: lowregressionIndicates than the issue relates to something that worked in a previous versionIndicates than the issue relates to something that worked in a previous versionstate: confirmedtype: bug/fix
Activity
JoostK commentedon Jul 10, 2019
robisim74 commentedon Jul 10, 2019
I had not noticed the removal of
emitDecoratorMetadata. However, no, it still doesn't work.alexzuza commentedon Jul 11, 2019
Appears the reason here is that Angular places
ngComponentDefgenerated code before all__decoratecalls for class.ɵɵdefineComponentfunction takesonInitas a reference toClass.prototype.ngOnInitand overridding it later has no any effect.angular/packages/core/src/render3/definition.ts
Line 264 in 58be2ff
Here's a pseudo-code of what happens:
In case of JIT compilation
componentDefis created lazily as soon asgetComponentDefgets called. It happens later.alxhub commentedon Jul 16, 2019
Yeah, this is unfortunately WAI.
Angular's compiler btw has no say in the location of
ngComponentDefvs the__decoratecall - this is the way TypeScript naturally compiles static fields. See this example. The time at which the lifecycle hook is read off the class is an implementation detail of the runtime, and the way we do things now in Ivy is an optimization.50 remaining items
shadow1349 commentedon Jun 12, 2020
tinganho commentedon Jun 12, 2020
ramtob commentedon Jun 14, 2020
See above, there is a PR on this problem.
khiami commentedon Jun 27, 2020
Any update after v10 is out?
hakimio commentedon Jun 27, 2020
@khiami you can track progress in PR #35464
TinyMan commentedon Jul 3, 2020
I found out that this workaround create errors in JIT if your decorator is after
@Component()like this:because (my guess from debugging it)
ɵcmpin JIT is a getter that compiles the component. But when the decorators are executed the compiler is not ready (I think imported modules are not yet availables, as I had errors on ngIf not being an input on div).So the workaround is to check that we are in AOT before getting the
ɵcmpproperty :And this works both in AOT and JIT.
Also if you put your decorators after
@Component()(so that yours is executed first), it works becauseɵcmpis not yet defined.Needed this because AOT is still unusable for development with large project.
wsdt commentedon Aug 20, 2020
Using custom decorators with AoT and Ivy is also high priority for us.
hakimio commentedon Aug 20, 2020
@wsdt This was already fixed in Angular v10.0.5.
wsdt commentedon Aug 20, 2020
@hakimio Thank you ! :-)
kumaresan-subramani commentedon Aug 20, 2020
HI @hakimio ,
I think issue not get fixed still facing same issue.
My angular packages:
hakimio commentedon Aug 20, 2020
@kumaresan-subramani just use ngx-observable-lifecycle if you can not get it to work.
robisim74 commentedon Aug 26, 2020
As reported by @hakimio, after over a year this issue was fixed in Angular v10.0.5. If there were other problems, I think more specific issues should be open, and therefore I am closing it.
angular-automatic-lock-bot commentedon Sep 26, 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.