/*
* getCurrentInstance: 获取上下文和全局挂载实例
* ComponentInternalInstance: 实例对象类型
*/
import { ComponentInternalInstance, getCurrentInstance } from 'vue'
export default function useCurrentInstance() {
if (!getCurrentInstance()) {
throw new Error('useCurrentInstance() can only be used inside setup() or functional components!')
}
const { appContext } = getCurrentInstance() as ComponentInternalInstance
const proxy = appContext.config.globalProperties
return {
proxy,
}
}
然后
import useCurrentInstance from '@/utils/useCurrentInstance'
const { proxy } = useCurrentInstance()
proxy.md5(form.password)
main.js
import md5 from 'js-md5'
app.config.globalProperties.md5 = md5;