[vue3]does not exist on type 'ComponentPublicInstance.md

Property '$test' does not exist on type 'ComponentPublicInstance

eviroment

vue3 + ts

problem

期望使用全局變量

// main.ts
app.config.globalProperties.$test = 123

// test.vue
console.log(`this.$test`, this.$test)

error

Property '$test' does not exist on type 'ComponentPublicInstance

solution

定義$test的類型

創建文件 src/shims-core.d.ts

import { ComponentCustomProperties } from "vue";

declare module "@vue/runtime-core" {
    interface ComponentCustomProperties {
      $test: number,
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章