typescript - 如何从同一个打字稿文件扩展一个类?
问题描述
我正在尝试继承同一文件中的类,但编译器会生成以下错误:
类 'FilterController' 在其声明之前使用。ts(2449) FilterData.ts(53, 7): 'FilterController' 在这里声明。
(属性)FilterController._typingValue:HTMLInputElement 只有基类的公共和受保护方法才能通过“超级”关键字访问。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
扩展第一个类。
推荐阅读
- linux - 如何在引导徽标上隐藏 linux 引导日志?
- delphi - 使用 THTTPRIO 进行 SOAP 调用时如何强制使用 TLS 1.2?
- android - Android - 在完成第一个活动之前打开另一个活动时未调用 onActivityResult
- python - 如何将 message.text 转换为字符串并将其保存到数据库。如何通过电报机器人下载照片。TelegramBotAPI
- flutter - Flutter:如何调用存储在变量中的类
- autodesk-forge - How to receive the RealityCapture results on an email address
- javascript - 当值 <0 时需要红色的进度条
- svg - 使用 imagemagick 从带有覆盖文本的 SVG 创建 PNG 图标
- flutter - 如何存根不属于类的函数
- flutter - Flutter - 无法在文本字段中编辑文本而不点击它