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
變量了