[英]How to fix "Property XX does not exist on type 'CombinedVueInstance" errors in VSCode? (Vue with Typescript)
VS Code is reporting a lot of problems/red lines when editing my Vue Typescript files:在编辑我的 Vue Typescript 文件时,VS Code 报告了很多问题/红线:
Example error:示例错误:
[ts] Property 'isLoading' does not exist on type 'CombinedVueInstance<Vue,
object, > object, object, Readonly<Record<never, any>>>'. [2339]
The problem seems to come when I reference a property on "this", and all such references have a red line in the editor stating a variant of the error above.当我在“this”上引用一个属性时,问题似乎来了,并且所有此类引用在编辑器中都有一条红线,说明上述错误的变体。 The problem is the same for both properties defined in Vue's data object and functions defined in methods.
Vue 的数据 object 中定义的属性和方法中定义的函数都是一样的问题。
Now, there are two interesting aspects:现在,有两个有趣的方面:
Info about my setup:有关我的设置的信息:
tsconfig.js: tsconfig.js:
{
"compilerOptions": {
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"allowJs": true,
"target": "es5",
"strict": true,
"module": "es6",
"moduleResolution": "node",
"outDir": "../../../dist/public/js",
"sourceMap": true
}
}
I have tried the following:我尝试了以下方法:
I am now completely stumped - and hope someone can help me...我现在完全被难住了 - 希望有人能帮助我......
Code example below (all this. references have a red line in my VS Code):下面的代码示例(所有 this.references 在我的 VS Code 中都有一条红线):
import axios from "axios";
import Vue from "vue";
// tslint:disable-next-line no-unused-expression
new Vue({
computed: {
hasProvider(): boolean {
// this line throw two errors
return this.isLoading === false && this.providerList.length > 0;
},
},
data() {
return {
description: "",
id: "",
isLoading: true,
name: "",
providerList: [],
};
},
el: "#app",
methods: {
loadProviderList() {
axios
.get("/api/provider/all")
.then((res: any) => {
// these lines throw an error
this.providerList = res.data.items;
this.isLoading = false;
})
.catch((err: any) => {
// tslint:disable-next-line:no-console
console.log(err);
});
}
},
mounted() {
// this line throw an error
return this.loadProviderList();
}
});
Came across this error at work today (Vue without TypeScript in VSCode).今天在工作中遇到了这个错误(VSCode 中没有 TypeScript 的 Vue)。
It also came out of nowhere for us, and in the end the culprit was just that the Vetur extension in VSCode needed to be reloaded.它对我们来说也是无中生有,最终罪魁祸首只是需要重新加载 VSCode 中的 Vetur 扩展。
在 settings.json 中,设置:
"vetur.experimental.templateInterpolationService": false
Seems the issue was with the return statement in my mounted() stage.似乎问题出在我的 mount() 阶段的 return 语句上。 Removing that return (probably something leftover from an example I was using) solved the problem.
删除该返回(可能是我使用的示例中剩余的东西)解决了问题。
mounted() {
// this works
this.loadProviderList();
}
Still, strange that the environment changed behaviour from one day to the next, and that it compiled - might never get an answer to that :)尽管如此,奇怪的是环境从一天到第二天改变了行为,并且它编译 - 可能永远不会得到答案:)
I solved this issue with:我解决了这个问题:
tsconfig.json
{
"compilerOptions": {
"noImplicitThis": false
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.