Skip to content
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

NG0204: Token InjectionToken X is missing a ɵprov definition after Angular 15 upgrade #218

Closed
akoreh opened this issue Nov 29, 2022 · 20 comments

Comments

@akoreh
Copy link

akoreh commented Nov 29, 2022

After updating to Angular 15, components with the following setup fail with NG0204: Token InjectionToken X is missing a ɵprov definition.

import {
  ChangeDetectionStrategy,
  Component,
  InjectionToken,
  Input,
} from '@angular/core';
import { UntilDestroy } from '@ngneat/until-destroy';

export const FAB_ITEM = new InjectionToken<any>('FAB_ITEM');

@UntilDestroy()
@Component({
  selector: 'aw-fab-item',
  templateUrl: 'fab-item.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  providers: [{ provide: FAB_ITEM, useExisting: FabItemComponent }],
})
export class FabItemComponent {
  @Input() disabled = false;
}

The error dissapears and the component works after removing the UntilDestroy decorator.

@akoreh akoreh changed the title NG0204: Token InjectionToken XXX is missing a ɵprov definition after angular 15 upgrade NG0204: Token InjectionToken X is missing a ɵprov definition after angular 15 upgrade Nov 29, 2022
@akoreh akoreh changed the title NG0204: Token InjectionToken X is missing a ɵprov definition after angular 15 upgrade NG0204: Token InjectionToken X is missing a ɵprov definition after Angular 15 upgrade Nov 29, 2022
@arturovt
Copy link
Collaborator

Could you please provide a minimal reproducible example?

@arturovt
Copy link
Collaborator

Closing as not reproducible. We have an integration app in this repository which is running with Angular 15.

Feel free to re-open the issue once you're able to provide a reproducible example.

@Wykks
Copy link

Wykks commented Dec 13, 2022

Got the same issue, same fix. No idea what's going on. I'm not able to reproduce it on stackblitz...

@anschm
Copy link

anschm commented Dec 16, 2022

Got the same issue after migration to angular 15.

@anschm
Copy link

anschm commented Dec 19, 2022

I saw that the repo was upgraded to angular 15. Could we get a new release? Maybe this fix the issue.

@arturovt
Copy link
Collaborator

Well, I could’ve published a new version but I’d want to double check to ensure this is related to a new version. Because that might be an unnecessary publish. I need a reproducible example gentlemen.

@anschm
Copy link

anschm commented Dec 19, 2022

Could you publish a dev version so that I can check against my project?

@NetanelBasal
Copy link
Member

I'm using Angular v15, and I don't have an issue. It'll be helpful if you can reproduce it.

@jogelin
Copy link

jogelin commented Dec 21, 2022

I have had something similar since the upgrade to Angular 15.

We have a use case where we want to get a MatFormFieldControl from a ControlValueAccessor component implementing the MatFormFieldControl that is injected using the Content Projection.

  @ContentChild(MatFormFieldControl, { static: true })
  private formFieldControl!: MatFormFieldControl<unknown>;

  ngOnInit(): void {
    this.matFormField._control = this.formFieldControl;

For MatFormFieldControl components that have the UntilDestroy() decorator, the this.formFieldControl is empty
When you remove the decorator, it works well again.

I'll try to provide a reproducible case today

@jogelin
Copy link

jogelin commented Dec 22, 2022

I was not able to reproduce our issue on a clean project TBH.

We are still investigating why it is failing by removing all codes around.

Even if I remove the code within the decorator it is failing.

Just the fact that we set the decorator has an impact on our @ContentChild reaction.

I think this is related to angular/angular#48276 but no idea what is the reason.

@jogelin
Copy link

jogelin commented Dec 22, 2022

Even trying that approach it fails:

function UntilDestroy(options = {}) {
  return (type: unknown) => {};
}

@UntilDestroy()
@Component({
...

So at least, this is not related to this library ;)

@arturovt
Copy link
Collaborator

To be honest I have issues in other libraries as well related to decorators. But it’s usually not a library issue but something related to the application.

Since I’ve seen already some issues related to decorators this means there actually has been some change in the TS compiler or Angular compiler that might affect it.

@mcaverick
Copy link

this typescript issue seems to be related

@jogelin
Copy link

jogelin commented Dec 22, 2022

It is indeed related to a modification of the way properties are set as you can see in the Typescript documentation. This new way is applied when your target is es2022.

Angular cli is aware of this as you can this in this code , it will set the correct value

The solution then is to set the target to es2021 then Angular Cli will hook the configurations and set es2022 and useDefineForClassFields to false

There is only one thing that we still don't understand is that if you set these values yourself to avoid the warning of Angular Cli, then it doesn't work. It seems typescript will read directly the file and just ignore useDefineForClassFields

Related tickets:

@arturovt
Copy link
Collaborator

Yeah, I haven't got my hands yet to going through the Angular 15 release completely. In other libraries this is being solved by playing around with useDefineForClassFields.

@arturovt
Copy link
Collaborator

arturovt commented Dec 27, 2022

Have to re-open the issue because I also experienced it in one of my apps (haven't met it earlier):

Error: NG0204: Token InjectionToken NgValueAccessor is missing a ɵprov definition.

Not sure how to fix it yet, will be looking for possible resolutions.

UPD: I ain't sure this is exactly related to this library.

@arturovt arturovt reopened this Dec 27, 2022
@arturovt
Copy link
Collaborator

@jogelin I have experienced this issue with a control value accessor too. I have fixed it with the forwardRef.

Before:

provide: NG_VALUE_ACCESSOR,
useExisting: CosAutocompleteComponent

After:

provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CosAutocompleteComponent),

Your thoughts?

P.S. I know it wasn't necessary to use forwardRef since Ivy has been released (because component definition appeared after the class itself, thus there has been no reason to "capture" the class reference).

@arturovt
Copy link
Collaborator

Ok, I just doubled checked my app and the issue wasn’t related to this library. Sorry for pinging anyone who’s subscribed to this issue.

@jogelin
Copy link

jogelin commented Jan 4, 2023

@jogelin I have experienced this issue with a control value accessor too. I have fixed it with the forwardRef.

Before:

provide: NG_VALUE_ACCESSOR,
useExisting: CosAutocompleteComponent

After:

provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CosAutocompleteComponent),

Your thoughts?

P.S. I know it wasn't necessary to use forwardRef since Ivy has been released (because component definition appeared after the class itself, thus there has been no reason to "capture" the class reference).

Seems to work too :)

@sagrawal31
Copy link

I'm still able to reproduce the issue in Angular v15.2. Though, I'm not sure if I have to follow this approach #224 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants