在main.ts中使用了app.config.globalProperties.$xxx = xxx 来全局挂载数据。
但是在使用getCurrentInstance来获proxy对象时不存在挂载的方法。于是换了一种思路,将方法挂载到window上,不仅解决了诸多问题,还更好用。
第一步:main.ts中
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
//ui
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
//引入并挂载方法
import Lodop from "@/utils/LodopFuncs";
Lodop().then(res => {
window.$Lodop = res
})
const app = createApp(App)
app.use(store).use(router).use(ElementPlus).mount('#app')
此时会出现错误也不能直接去hooks,或者组件中使用,会出现错误提示:
类型“Window & typeof globalThis”上不存在属性“$Lodop”。
第二步: 需要在shims-vue.d.ts中添加以下代码:
//shims-vue.d.ts
interface Window {
$loading: any
$message: any
$dialog: any
$Lodop: any
$vue: any
}
第三步:可以在hooks,或组件中使用了
const Lodop = window.$Lodop