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

如圖所示

2.解決辦法

在組件頂部聲明一個declare

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

declare module vue/types/vue

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章