iviewi+vue弹出多个message的问题处理

重写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: '提示内容'});
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值