question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

using decorator with component providers which uses a function breaks the component

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
arturovtcommented, Oct 8, 2020

Well, seems like settings "target": "es2015" in tsconfig solved the issue. it was es5

but, why?

Closing.

Targetting ES2015 emits top-level variable that references your class:

var UserSelectComponent_1;

function createFormProvider(component) { ... }

let UserSelectComponent = (UserSelectComponent_1 = class UserSelectComponent {
});

setClassMetadata(UserSelectComponent, [
  ...,
  {
    providers: [createFormProvider(UserSelectComponent_1)]
  }
])

UserSelectComponent_1 is defined in this case before the function is invoked.

ES5 emitted code looks as following:

function createFormProvider(component) { ... }

var UserSelectComponent = (function () {
  function UserSelectComponent() {}
  UserSelectComponent_1 = UserSelectComponent;
  var UserSelectComponent_1;
  return UserSelectComponent;
})();

setClassMetadata(UserSelectComponent, [
  {
    providers: [createFormProvider(UserSelectComponent_1)]
  }
])

Notice that it references the wrong variable which is declared inside the self-invoked function.

0reactions
danzroucommented, Oct 8, 2020

Thanks for the info @arturovt

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found