ngneat / until-destroy

šŸ¦Š RxJS operator that unsubscribe from observables on destroy
https://netbasal.com/
MIT License
1.74k stars 100 forks source link

Component is undefined if @UntilDestroy() is used #224

Closed anschm closed 1 year ago

anschm commented 1 year ago

I wrote a method subformComponentProviders which generates the providers for the value accessor pattern. The method is implemented like:

export function subformComponentProviders(component: any): {
  provide: InjectionToken<ControlValueAccessor>;
  useExisting: Type<any>;
  multi?: boolean;
}[] {
  return [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => component),
      multi: true
    },
    {
      provide: NG_VALIDATORS,
      useExisting: forwardRef(() => component),
      multi: true
    }
  ];
}

After migrating to angular 15.1.1 and the newest until-destroy version in my case the component parameter from the method subformComponentProviders is undefined if I put @UntilDestroy() on my component. Its used like:

@UntilDestroy()
@Component({
  selector: 'chips-autocomplete-input',
  templateUrl: './chips-autocomplete-input.component.html',
  styleUrls: ['./chips-autocomplete-input.component.scss'],
  providers: subformComponentProviders(ChipsAutocompleteInputComponent),
  changeDetection: ChangeDetectionStrategy.OnPush
})

If I delete @UntilDestroy() the subformComponentProviders gets the component.

arturovt commented 1 year ago

This is not an issue of this library, see https://github.com/ngneat/until-destroy/issues/218

arturovt commented 1 year ago

Your code is also not correct actually, the forwardRef should not be used in the function, but outside when calling this function. It fails because thereā€™s a change in the TS compiler for decorators. If you ā€œcaptureā€ the reference through forwardRef when calling the function it should work.