vue3 + ts 如何使用elmentPlus中的全局注册函数

2023-11-20

在vue2 中 我们在main.js中引入elementUI use挂载后可以直接在页面中通过this.$message去调用element中的方法,而vue3这种不再适用

我们可以在utils 中 新建useCurrentInstance.ts 文件

import { ComponentInternalInstance, getCurrentInstance } from 'vue'
export default function useCurrentInstance() {
  const { appContext } = getCurrentInstance() as ComponentInternalInstance
  const proxy = appContext.config.globalProperties
  return {
    proxy
  }
}

在需要使用element中$message,$confirm的地方引入

import useCurrentInstance from '@/utils/useCurrentInstance'
const { proxy } = useCurrentInstance()

之后通过proxy.$message的方式去调用即可

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

vue3 + ts 如何使用elmentPlus中的全局注册函数 的相关文章

随机推荐