简体   繁体   English

如何修复 VSCode 中的“Property XX does not exist on type 'CombinedVueInstance”错误? (Vue 与打字稿)

[英]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:现在,有两个有趣的方面:

  1. I had no issue with these files until today.直到今天,我对这些文件没有任何问题。 Yesterday, before shutting down, everything was working as intended.昨天,在关闭之前,一切都按预期进行。 Restarting today I got this issue.今天重新启动,我遇到了这个问题。
  2. The code compiles and runs.代码编译并运行。 If I build the files using tsc, they compile nicely - and the application deploys and works as intended.如果我使用 tsc 构建文件,它们会很好地编译 - 并且应用程序会按预期部署和工作。

Info about my setup:有关我的设置的信息:

  • npm view typescript version gives me version 3.2.4 npm 查看 typescript version 给我的版本是 3.2.4
  • Vue is at 2.5.22 Vue 在 2.5.22
  • VS Code is at 1.30.2. VS Code 为 1.30.2。

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:我尝试了以下方法:

  • Reinstalling Typescript and Vue重装Typescript和Vue
  • Restarting VS Code numerous times多次重启 VS Code
  • Restarting TSServer manually手动重启 TSServer
  • Restarting the computer重新启动计算机

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.

相关问题 这不是 vue 中的类型 CombinedVueInstance with typescript - this not of type CombinedVueInstance in vue with typescript Nuxt 插件属性在“CombinedVueInstance”类型上不存在 - Nuxt plugin property does not exist on type 'CombinedVueInstance 类型“CombinedVueInstance”上不存在属性“XXX” <Vue, {}, {}, {}, Readonly<Record<never, any> &gt;&gt;&#39; - Property 'XXX' does not exist on type 'CombinedVueInstance<Vue, {}, {}, {}, Readonly<Record<never, any>>>' 使用Jest测试Vuejs和TypeScript会导致错误“属性&#39;xxx&#39;在类型&#39;CombinedVueInstance&#39;中不存在” - Testing Vuejs and TypeScript with Jest causes an error "Property 'xxx' does not exist on type 'CombinedVueInstance' React Native TypeScript 类型“xx”上不存在属性“xxx” - React Native TypeScript Property 'xxx' does not exist on type 'xx' NativeScript-Vue with Typescript in VSCode with Vetur: Alert with then giving Property 'then' does not exist on type 'void' - NativeScript-Vue with Typescript in VSCode with Vetur: Alert with then giving Property 'then' does not exist on type 'void' 如何修复 typeScript 中的“对象”类型上不存在“属性”拨号 - how to fix 'Property 'dials' does not exist on type 'Object' in typeScript 带有打字稿的 Vue2,类型上不存在属性 - Vue2 with typescript, Property does not exist on type 如何修复 Typescript 中的“元素”类型错误不存在属性? - How to fix property does not exist on type 'Element' error in Typescript? 如何解决Typescript中“ object”类型上不存在的“属性”字符串问题 - How To fix 'Property “string” does not exist on type “object”' error in Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM