-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom decorators not working using AoT and Ivy #31495
Comments
This comment has been minimized.
This comment has been minimized.
I had not noticed the removal of |
Appears the reason here is that Angular places
Here's a pseudo-code of what happens:
In case of JIT compilation |
Yeah, this is unfortunately WAI. Angular's compiler btw has no say in the location of |
Transferring this to core as there's nothing we can do on this from the compiler side - the behavior in question is the snapshotting of |
Any update on this issue? |
Given a class decorator function Mixin(value) {
return function(target) {
target.prototype.ngOnInit = function () {
console.log("ngOnInit", value)
}
}
}
@Component({ ... })
@Mixin({ value: 123 })
export class AppComponent {} This workaround should work in Ivy const Extend = Mixin({ value: 123 })
@Component({ ... })
export class AppComponent {
static mixins = Extend(AppComponent)
} For ViewEngine compatibility, you also need to add a stub for ngOnInit @Component({ ... })
export class AppComponent {
public ngOnInit?()
static mixins = Extend(AppComponent)
} Assigning to a static property on the class forces the mixin to be executed before the runtime snapshots the class. |
This comment has been minimized.
This comment has been minimized.
@kumaresan-subramani From within your empty lifecycle methods, you should delegate into the mixin field. |
Hi @JoostK , Any update on this? I have tried with latest v9.0.0-rc.4 but still not working |
HI @JoostK , Can you please provide timeline for this issue? |
@kumaresan-subramani the team doesn't normally provide ETAs or timelines for issue resolution. Based on the milestone, this is not being considered for version 9 final. It's possible that it could go into a 9.x minor or patch, be pushed to version 10, or it could be pushed even later. If you feel like the priority needs to be raised, please provide a clear use case that demonstrates a significant issue that does not have a workaround. If this is a problem for a Syncfusion library, please explain the issue, impact, and workarounds attempted. |
reason - #30818 (comment) Hi @Splaktar , you can find reasons above Workaround attempted |
@abadakhshan Please see #35314 (comment) for how to resolve the problem. The issue is closed because the framework is working as intended. |
It appears the issue I was running into was the same one that @abadakhshan describes here. My app makes heavy use of dynamically instantiated components that often times aren't referenced by the app other than in the NgModule declarations. It appears the reason my custom decorator isn't working is because those components are being tree-shaken from the build. So in my situation this isn't a defect of angular, but rather an education on Ivy... |
Any updates on this? We cannot move our project to ng9. |
This is a truly awful problem. Please fix it quickly as the version up of the internal project cannot be done. |
Please take a look at #35464. Hopefully this might resolve the issue for you? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
See above, there is a PR on this problem. |
Any update after v10 is out? |
I found out that this workaround create errors in JIT if your decorator is after @CustomDecorator()
@Component({ ...})
export class Comp {} because (my guess from debugging it) So the workaround is to check that we are in AOT before getting the export function getLifecycleHook(instanceConstructor: Function, hook: 'ngOnDestroy' | 'ngOnInit' | 'ngOnChanges') {
const desc = Object.getOwnPropertyDescriptor(instanceConstructor, ɵNG_COMP_DEF);
if (!!desc && !desc.get) { // check for ivy AND aot
// Using private API until the corresponding Angular issues are resolved.
// Issues: https://github.com/angular/angular/issues/31495, https://github.com/angular/angular/issues/30497
// PR: https://github.com/angular/angular/pull/35464
return instanceConstructor[ɵNG_COMP_DEF]['o' + hook.slice(3)];
} else {
return instanceConstructor.prototype[hook];
}
} And this works both in AOT and JIT. Also if you put your decorators after Needed this because AOT is still unusable for development with large project. |
Using custom decorators with AoT and Ivy is also high priority for us. |
@wsdt This was already fixed in Angular v10.0.5. |
@hakimio Thank you ! :-) |
HI @hakimio , I think issue not get fixed still facing same issue. My angular packages:
|
@kumaresan-subramani just use ngx-observable-lifecycle if you can not get it to work. |
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. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
🐞 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:used as:
🔬 Minimal Reproduction
angular-ivy-decorators
🔥 Exception or Error
No errors during compilation or in console. The
ngOnInit
method in decorator function is ignored.🌍 Your Environment
Angular Version:
Anything else relevant?
Tried without success also the next version of Angular.
Thanks
The text was updated successfully, but these errors were encountered: