重写message
resetMessage.js:
import {Message} from 'iView'
let messageInstance = null;
const resetMessage = (options) => {
if(messageInstance) {
Message.destroy()
console.log(1)
}else{
Message[options.type](options.message)
}
messageInstance = true
setTimeout(() => {
messageInstance = null
}, 5000);
};
['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
iview实现方式 node_modules\iview\src\components\message\index.js
export default {
name: 'Message',
info (options) {
return this.message('info', options);
},
success (options) {
return this.message('success', options);
},
warning (options) {
return this.message('warning', options);
},
error (options) {
return this.message('error', options);
},
loading (options) {
return this.message('loading', options);
},
message(type, options){
if (typeof options === 'string') {
options = {
content: options
};
}
return notice(options.content, options.duration, type, options.onClose, options.closable, options.render);
},
config (options) {
if (options.top || options.top === 0) {
defaults.top = options.top;
}
if (options.duration || options.duration === 0) {
defaults.duration = options.duration;
}
},
destroy () {
let instance = getMessageInstance();
messageInstance = null;
instance.destroy('ivu-message');
}
};
element-ui+vue
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
element实现方式 node_modules\element-ui\packages\message\src\main.js
main.js调用:
import {message} from './resetMessage';
Vue.use(ElementUI);
Vue.prototype.$message = message;
组件使用:
this.$message.error('提示内容');
this.$message({type:success,message:'提示内容'});
在js文件中调用:
import {message} from './resetMessage';
message.error('提示内容');
message({type: 'error',message: '提示内容'});