using decorator with component providers which uses a function breaks the component
See original GitHub issueI have a function that creates a form provider:
export function createFormProvider<T>(component: Type<T>): ExistingProvider {
return {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => component),
multi: true
};
}
It being widely used across the app.
When I decorate a component which uses it with @UntilDestroy()
, the component breaks with the following:
Uncaught ReferenceError: UserSelectComponent_1 is not defined
@UntilDestroy()
@Component({
selector: 'user-select',
templateUrl: './user-select.component.html',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [createFormProvider(UserSelectComponent)]
})
export class UserSelectComponent extends ControlValueAccessor<string | string[]> implements OnInit, AfterViewInit, OnDestroy {
...
}
However, when using the same but without a function in providers, it works as expected:
@UntilDestroy()
@Component({
selector: 'user-select',
templateUrl: './user-select.component.html',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UserSelectComponent), multi: true }
]
})
export class UserSelectComponent extends ControlValueAccessor<string | string[]> implements OnInit, AfterViewInit, OnDestroy {
...
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top Results From Across the Web
Angular 5: Using Service from inside Custom Decorator Function
Class decorator is executed once on class definition. In order to avoid race condition when calling AppModule.injector.get(LoggingService) ...
Read more >Dependency injection in action - Angular
The providers array is a property of the @Component() decorator function which must appear above the class definition. Break the circularity with forwardRef...
Read more >Angular Dependency Injection: Complete Guide
Everything that you need to know in practice to use the Angular dependency injection system, all in one place.
Read more >Decorators - Storybook - JS.ORG
A decorator is a way to wrap a story in extra “rendering” functionality. Many addons define decorators to augment your stories with extra...
Read more >Chapter 4, Understanding and Using Angular Components
We have been working so far by using the component class's functions within the context of the template. But there are use cases...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Targetting ES2015 emits top-level variable that references your class:
UserSelectComponent_1
is defined in this case before the function is invoked.ES5 emitted code looks as following:
Notice that it references the wrong variable which is declared inside the self-invoked function.
Thanks for the info @arturovt