为什么编译器会提示,找不到装饰器中的方法呢!
来源:1-1 课程导学

乔刻力
2022-05-09
Property ‘commonMethod’ does not exist on type ‘Test’.
function ClassFunctionExtends
<T extends { new(...args: any[]): any }>
(mytargetClass: T) {
console.log("mytargetClass", mytargetClass);
return class extends mytargetClass {
constructor(...args: any[]) {
super(args);
console.log("SonClass执行结束");
}
commonMethod() {
//console.log("this:", this)
console.log("name:", this.name)
}
}
}
@ClassFunctionExtends
class Test {
name!: string;
age!: number
// 1.先执行原来构造函数
constructor(name: string) {
this.name = name;
console.log("执行");
//console.log("beforendame:", this.name)// lisi
}
eat() {
console.log(this.name, "吃饭");
}
}
let theTest= new Test ("王五")
theTest.commonMethod()
写回答
1回答
-
keviny79
2022-05-10
装饰器是在运行后才绑定到类上的, 编译期间只对装饰器使用语法进行检测,但对 目标test对象无法识别到装饰器内部方法。
00
相似问题