如何从相同的打字稿文件扩展一个类?

时间:2019-03-22 18:56:08

标签: typescript

我正在尝试继承同一文件中的类,但是编译器会生成以下错误:

  

在声明之前使用的类'FilterController'.ts(2449)   FilterData.ts(53,7):在这里声明了'FilterController'。

     

(属性)FilterController._typingValue:HTMLInputElement仅公共   基类的受保护方法可以通过“ super”访问   keyword.ts(2340)

export default class FilterData extends FilterController {
    private _dataValue: any;

    constructor(protected storageKey: string, protected typingValue: HTMLInputElement, protected containerMain: HTMLElement, protected listboxMain: HTMLElement, protected listboxSecondary: HTMLElement) {
        super(storageKey, typingValue, containerMain, listboxMain, listboxSecondary);
    }

    /*private set data(dataValue: any) { this._dataValue = dataValue; }
    private get data(): any { return this._dataValue; }*/

    public initComponent() :void {
        this.keypress();
    }

    private keypress() {
        super._typingValue.addEventListener('keyup', (_event: KeyboardEvent) => {
            //this.search(_event);
            alert("aee")
        });
    }


}

class FilterController {
    protected readonly _storageKey: string;
    protected readonly _typingValue: HTMLInputElement;
    protected readonly _containerMain: HTMLElement;
    protected readonly _listboxMain: HTMLElement;
    protected readonly _listboxSecondary: HTMLElement;

    constructor(protected storageKey: string, protected typingValue: HTMLInputElement, protected containerMain: HTMLElement, protected listboxMain: HTMLElement, protected listboxSecondary: HTMLElement){
        this._storageKey = storageKey;
        this._typingValue = typingValue;
        this._containerMain = containerMain;
        this._listboxMain = listboxMain;
        this._listboxSecondary = listboxSecondary;
    }
}

1 个答案:

答案 0 :(得分:0)

该错误消息表明您在声明一个类之前正在使用它。在这种情况下,您要在类的声明之前扩展FilterController

最简单的解决方案是更改两个类的顺序。首先声明FilterController,然后声明FilterData,以扩展第一类。