项目场景:
vue3+ts
问题描述
在main.ts中注册了一个全局的方法,由于vue3 setup语法糖中没有this无法使用的问题
//引用
import {message} from "ant-design-vue";
//全局注册
app.config.globalProperties.$message = message;
解决方案:
使用 vue 中的 getCurrentInstance 来获取组件实例
vue2在main.js中注册的方法可以在页面中直接用过 this 访问到是因为有组件实例,而在vue3 setup中的数据可以直接使用,所以并不需要组件实例,需要组件实例可以通过 ** getCurrentInstance ** 来获取
import {getCurrentInstance} from "vue";
const {proxy}:any = getCurrentInstance();
//console.log(proxy.$message)