In the new version of Element, the message prompt has been modified to correspond to one message for each request, which leads to the problem of multiple message prompt co-existing. In many cases, the content of these multiple message prompt is the same, which is not friendly in terms of user experience
Let’s start with 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
Copy the code
The second step is to import in main.js
import { message } from './assets/js/resetMessage';
Vue.use(ElementUI);
Vue.prototype.$message = message; // Overwrite message prompt must be placed after vue.use (ElementUI)
Copy the code
Called in vUE component code:
This.$message.error(' message content '); //way1
This.$message({type:success,message:' message '}); //way2
Call from a js file:
import { message } from '@/utils/resetMessage'; Message ({type: 'error',message: 'message content '}); //way1 message.error(' prompt content '); //way2Copy the code
This solves the problem of having multiple prompts on the same page
The above is a simple record of the development process, if there are inappropriate welcome to correct!