在main.js 注册全局组件(prototype)可以访问的属性
vue2.0
Vue.prototype.$Msg = (info, msgType) => {
ElMessage ({ message: info, type: msgType, customClass: 'm-z-index' })
}
vue3.0
const app = createApp(App)
app.config.globalProperties.$Msg = (info, msgType) => {
ElMessage ({ message: info, type: msgType, customClass: 'm-z-index' })
}
在vue中使用 msg全局属性
vue2.0
this.$Msg('提示内容','error')
vue3.0
在vue3.0 setup无法使用this(setup 是vue还没有创建前就执行的所以无法使用this)
import {reactive,toRefs,getCurrentInstance } from 'vue'
setup(){
const { proxy } = getCurrentInstance();
proxy 就是组件实例化对象
proxy.$Msg(‘提示内容’, 'error');
}