首页 > 解决方案 > 如何修复 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 的数据对象中定义的属性和方法中定义的函数,问题是相同的。

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

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

关于我的设置的信息:

tsconfig.js:

{
    "compilerOptions": {
        "lib": [
            "es6",
            "dom"
        ],
        "noImplicitAny": true,
        "allowJs": true,
        "target": "es5",
        "strict": true,
        "module": "es6",
        "moduleResolution": "node",
        "outDir": "../../../dist/public/js",
        "sourceMap": true
    }
}

我尝试了以下方法:

我现在完全被难住了-希望有人可以帮助我...

下面的代码示例(所有这些。引用在我的 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();
    }
});

标签: typescriptvue.jsvisual-studio-codevetur

解决方案


今天在工作中遇到了这个错误(Vue without TypeScript in VSCode)。

它对我们来说也不知从何而来,最终罪魁祸首只是需要重新加载 VSCode 中的 Vetur 扩展。


推荐阅读