Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript-从类批注中访问类字段_Typescript_Vue.js - Fatal编程技术网

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,是的,我编译了。现在修好了,谢谢。问题代码不应包含