vue3 全局挂载公用方法

编程入门 行业动态 更新时间:2024-10-25 12:28:43

vue3 <a href=https://www.elefans.com/category/jswz/34/1765343.html style=全局挂载公用方法"/>

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 全局挂载公用方法

本文发布于:2024-03-13 01:47:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1732909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:全局   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!