1. 在utils 文件夹新建 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
2.调用(局部引入)
import {
message
} from '@/utils/resetMessage';
Vue.prototype.$message = message;
3.使用
this.$message.error('验证失败')
this.$message({
message: '验证失败',
type: 'error'
});