全局挂载公用方法"/>
vue3 全局挂载公用方法
挂着全局公用方法
场景:在main.js中封装elementui的message方法
1.vue3中如果想要在main.js中封装公用的方法需要现在实例的config.globalProperties下去挂载方法
const app = createApp(App)
const promptMy = function(message, code = 200) {app.config.globalProperties.$message({message: message,type: code == 200 ? 'success' : 'error',})
}
app.config.globalProperties.$promptMy = promptMy;
调用的时候需要在页面中引用
import {getCurrentInstance,
} from "vue";let { proxy } = getCurrentInstance();proxy.$promptMy(message, code);
每个不同的页面使用的时候都需要手动这样处理一下有点麻烦,个人换个思路将方法挂载在window下,需要调用的时候可直接使用(部分场景下可能存在问题再考虑)
const app = createApp(App)
const promptMy = function(message, code = 200) {app.config.globalProperties.$message({message: message,type: code == 200 ? 'success' : 'error',})
}
window.$promptMy = promptMy//调用
$promptMy(message, code);
更多推荐
vue3 全局挂载公用方法
发布评论