如何修复 VSCode 中类型“CombinedVueInstance”错误中的“属性 XX 不存在”?(带有打字稿的Vue)

Mar*_*nen 6 typescript vue.js visual-studio-code

VS Code 在编辑我的 Vue Typescript 文件时报告了很多问题/红线:

示例错误:

[ts] Property 'isLoading' does not exist on type 'CombinedVueInstance<Vue, 
object, > object, object, Readonly<Record<never, any>>>'. [2339]
Run Code Online (Sandbox Code Playgroud)

当我在“this”上引用一个属性时,问题似乎出现了,并且所有此类引用在编辑器中都有一条红线,说明上述错误的一个变体。对于 Vue 的数据对象中定义的属性和方法中定义的函数,问题都是一样的。

现在,有两个有趣的方面:

  1. 直到今天,我对这些文件都没有问题。昨天,在关闭之前,一切都按预期进行。今天重新启动,我遇到了这个问题。
  2. 代码编译并运行。如果我使用 tsc 构建文件,它们会很好地编译 - 并且应用程序会按预期部署和工作。

关于我的设置的信息:

  • npm 查看打字稿版本给了我 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
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了以下方法:

  • 重新安装 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();
    }
});
Run Code Online (Sandbox Code Playgroud)

小智 6

今天工作中遇到了这个错误(VSCode 中没有 TypeScript 的 Vue)。

它对我们来说也是突然出现的,最终的罪魁祸首只是 VSCode 中的 Vetur 扩展需要重新加载。


小智 6

在settings.json中设置:

"vetur.experimental.templateInterpolationService": false
Run Code Online (Sandbox Code Playgroud)


Mar*_*nen -1

问题似乎出在我的 Mounted() 阶段的 return 语句上。删除该返回(可能是我使用的示例中剩下的东西)解决了问题。

mounted() {
        // this works
        this.loadProviderList();
}
Run Code Online (Sandbox Code Playgroud)

尽管如此,奇怪的是环境一天又一天地改变了行为,并且它编译了 - 可能永远不会得到答案:)