简体   繁体   中英

Custom component using controlValueAccessor - ts-Lint error: component was used before defined

this is the provider used in my @component decorator of RatingInputComponent.

providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => RatingInputComponent),
      multi: true
    }
  ]

i am getting an ts-lint error: 'RatingInputComponent' was used before it was defined. but forwardRef Allows to refer to references which are not yet defined.

i am using Angular 9.1.3

whole ts file.

import { Component, forwardRef, Input } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

@Component({
    selector: 'rating-input',
    templateUrl: './rating-input.component.html',
    styleUrls: ['./rating-input.component.scss'],
    providers: [
        {
            provide: NG_VALUE_ACCESSOR,
            useExisting: forwardRef(() => RatingInputComponent),
            multi: true
        }
    ]
})
export class RatingInputComponent implements ControlValueAccessor {
    @Input() items: string[];
    @Input() defaultText = 'Select Item';

    public value: string;
    public isDisabled: boolean;

    private onChange;

    registerOnChange(fn: any): void {
        this.onChange = fn;
    }

    registerOnTouched(fn: any): void {
    }

    setDisabledState(isDisabled: boolean): void {
        this.isDisabled = isDisabled;
    }

    writeValue(value: any): void {
        this.value = value;
    }

    selectItem(item): void {
        this.onChange(item);
        this.value = item;
    }
}

在此处输入图像描述 any guidance appreciated.

The typescript code is valid, seems to be an issue with the linter. You can disable this lint rule by setting "no-use-before-declare": false .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM