【问题标题】:How to extend a class from the same typescript file?如何从同一个打字稿文件扩展一个类?
【发布时间】:2019-03-23 03:26:43
【问题描述】:

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

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

(property) FilterController._typingValue: HTMLInputElement Only public 基类的和受保护的方法可以通过“super”访问 关键字.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;
    }
}

【问题讨论】:

  • 正如错误所说,您尝试在文件中使用FilterController 之前您已在文件中定义它。只需将FilterController 移到FilterData 之前的顶部即可。
  • 更多关于 JS 中的类和提升的细节:stackoverflow.com/questions/35537619/…

标签: typescript


【解决方案1】:

错误消息表明您正在使用一个尚未声明的类。在你的情况下,你在类的声明之前扩展FilterController

解决问题的最简单方法是更改​​两个类的顺序。首先声明FilterController,然后是FilterData,它扩展了第一个类。

【讨论】:

    猜你喜欢
    • 2020-03-26
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 2010-10-27
    • 1970-01-01
    • 2018-05-30
    相关资源
    最近更新 更多