【发布时间】: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