vue3 +ts 自定義全局變量

1. 全局變量掛載

// vue2
Vue.prototype.$lm = {}

// vue3
const app = Vue.createApp({})
app.config.globalProperties.$lm = {}

2.聲明類型

使用 ts 的情況下,掛載完全局變量後,在 vue 文件中,通過 this 對象 . 出來提示,不存在該類型

我們需要聲明一下:

新建一個 xxx.d.ts 文件

//xxx.d.ts
export{} // 必須保留
declare module '@vue/runtime-core' {
    interface ComponentCustomProperties {
        $lm:any
    }
  }

ok,現在就可以通過 this 對象 .  $lm 變量了

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