【发布时间】:2018-05-02 15:37:24
【问题描述】:
我目前正在为项目编写一些内部抽象类,我需要它是通用的(我希望它是可扩展的)。
我希望调用我的类,因为它会扩展像 Sample extends T 这样的 T 模板,以便拥有 T 的所有参数。
例如,如果 T 是 Vue,我将拥有所有 Vue 参数,例如 $el 或 $options,而无需重新声明 Vue 也不包含它。
所以我有以下内容:
export namespace SDK {
export abstract class Sample<T> {
private static methods: any = {
hello: Sample.prototype.hello
}
abstract callMe () : void;
x: number = 0
y: number = 0
width: number = 1
height: number = 1
hello (): void {
console.log('Sample hello world')
this.callMe()
}
}
}
但我不知道如何处理将 T 的属性包含到 Sample 中。
我希望它是这样的:
export namespace SDK {
export abstract class Sample<T> {
private static methods: any = {
hello: Sample.prototype.hello
}
abstract callMe () : void;
x: number = 0
y: number = 0
width: number = 1
height: number = 1
// T properties (Vue example)
$el: HTMLElement
...
hello (): void {
console.log('Sample hello world')
this.callMe()
}
}
}
我希望我的班级被称为:
export default class MyComponent extends SDK.Sample<Vue> {
name: string = 'my-component';
callMe () : void {
console.log('called')
}
mounted () : void {
this.hello()
}
}
我没有找到任何关于从允许在其中包含参数的模板类扩展的信息。
【问题讨论】:
-
澄清一下:你想扩展 Sample
还是 Sample 扩展 T?他们是不同的 -
我想要 Sample 扩展 T.
标签: typescript vue.js