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.**

如图所示

2.解决办法

在组件顶部声明一个declare

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

declare module vue/types/vue

Logo

一站式虚拟内容创作平台,激发创意,赋能创作,进入R空间,遇见同道,让优质作品闪耀发光。​

更多推荐