vuejs+typescrpt this[key] 使用变量获取this属性报错解决办法Element implicitly has an 'any' type because

1.在使用vue+typescript 构建项目的时候,使用变量动态获取属性值的时候,ts解析器会报一个错误。

**

 var key = 'name';
 this[key] = 123;
 // 这里会提示一个语法错误  **Element implicitly has an 'any' type because type 'About' has no index signature.**
console.log(this[key]) 
// 这里会提示一个语法错误  **Element implicitly has an 'any' type because type 'About' has no index signature.**

vuejs+typescrpt this[key] 使用变量获取this属性报错解决办法Element implicitly has an 'any' type because_第1张图片

2.解决办法

在组件顶部声明一个declare

declare module 'vue/types/vue' {
    interface Vue {
        [key: string]: any,
    }
}

vuejs+typescrpt this[key] 使用变量获取this属性报错解决办法Element implicitly has an 'any' type because_第2张图片

你可能感兴趣的:(JS,typescript,vue,typescript,vuejs)