建立resetMessage.js文件
import { Message } from 'element-ui';
let messageInstance = null;
const resetMessage = (options) => {
if(messageInstance) {
messageInstance.close()
}
messageInstance = Message(options)
};
['error','success','info','warning'].forEach(type => {
resetMessage[type] = options => {
if(typeof options === 'string') {
options = {
message:options
}
}
options.type = type
return resetMessage(options)
}
})
export const message = resetMessage
在main.js文件引入:
import ElementUI from 'element-ui';
import { message } from './resetMessage'
Vue.use(ElementUI);
Vue.prototype.$message = message // 一定要放在Vue.use(ElementUI)后面,不然无效
调用方式和element一样