Typescript-从类批注中访问类字段
使用以下代码:Typescript-从类批注中访问类字段,typescript,vue.js,Typescript,Vue.js,使用以下代码: @Component({ props: { value: String }, mounted() { //Do something with `bar` this.bar = this.bar + " is now mounted"; } }) export default class Foo extends Vue { private bar : string = &q
@Component({
props: {
value: String
},
mounted() {
//Do something with `bar`
this.bar = this.bar + " is now mounted";
}
})
export default class Foo extends Vue {
private bar : string = "This element";
}
在typescript控制台中会出现以下错误,但在其他情况下代码运行正常
37:14 Property 'bar' does not exist on type 'Vue'.
05 | mounted() {
06 | //Do something with `bar`
07 | this.bar = this.bar + " is now mounted";
| ^
08 | }
09 | })
您需要为类
组件注释提供一个类型参数,以便编译器知道this.bar
确实存在
@Component<Foo>({
props: {
value: String
},
mounted() {
//Do something with `bar`
this.bar = this.bar + " is now mounted";
}
})
export default class Foo extends Vue {
private bar : string = "This element";
}
@组件({
道具:{
值:字符串
},
安装的(){
//用酒吧做点什么`
this.bar=this.bar+“现在已安装”;
}
})
导出默认类Foo扩展Vue{
专用栏:string=“This element”;
}
这段代码编译时没有错误。@IlmariKaronen-Oops,是的,我编译了。现在修好了,谢谢。问题代码不应包含