typescript - 如何修复 VSCode 中的“‘CombinedVueInstance’类型上不存在属性 XX”错误?(带有打字稿的Vue)
问题描述
VS Code 在编辑我的 Vue Typescript 文件时报告了很多问题/红线:
示例错误:
[ts] Property 'isLoading' does not exist on type 'CombinedVueInstance<Vue,
object, > object, object, Readonly<Record<never, any>>>'. [2339]
当我在“this”上引用一个属性时,问题似乎出现了,并且所有此类引用在编辑器中都有一条红线,说明上述错误的变体。对于 Vue 的数据对象中定义的属性和方法中定义的函数,问题是相同的。
现在,有两个有趣的方面:
- 直到今天我对这些文件没有任何问题。昨天,在关闭之前,一切都按预期工作。今天重新启动,我遇到了这个问题。
- 代码编译并运行。如果我使用 tsc 构建文件,它们会很好地编译 - 并且应用程序会按预期进行部署和工作。
关于我的设置的信息:
- npm view typescript version 给了我 3.2.4 版
- Vue 在 2.5.22
- VS Code 在 1.30.2。
tsconfig.js:
{
"compilerOptions": {
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"allowJs": true,
"target": "es5",
"strict": true,
"module": "es6",
"moduleResolution": "node",
"outDir": "../../../dist/public/js",
"sourceMap": true
}
}
我尝试了以下方法:
- 重新安装 Typescript 和 Vue
- 多次重启 VS Code
- 手动重启 TSServer
- 重新启动计算机
我现在完全被难住了-希望有人可以帮助我...
下面的代码示例(所有这些。引用在我的 VS 代码中有一条红线):
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();
}
});
解决方案
今天在工作中遇到了这个错误(Vue without TypeScript in VSCode)。
它对我们来说也不知从何而来,最终罪魁祸首只是需要重新加载 VSCode 中的 Vetur 扩展。
推荐阅读
- java - 通过反射运行码头网络服务
- hyperledger - “到期”:不是“uv_timer_s”的成员
- angular - # 基本路由在 Angular 6 中不起作用
- http - http PUT 请求在 golang 中上传 zip 文件
- python - Django - 注释要查询的最新子列值
- javascript - 如何解决 1.8.2 版本的 jquery msie 错误?
- ios - 在导航栏右侧添加两个按钮时出现错误
- android - 使用 Retrofit2 将图片上传到帖子正文中的服务器
- mysql - 如何在mysql中获取丢失的行
- c# - Filtering DataGridView via multiple combo boxes