手动关闭elementUI的messageBox

项目中遇到一个问题,后端会验证前端所有的请求,当登录已超时会返回相关的状态码,前端拿到登录超时的状态码时要弹出一个MessageBox,现在遇到的情况是,一个页面加载时会发两个请求,当用户已登录超时,会弹出两个MessageBox,关闭一个之后会跳到登录页,到登录页之后还会看到一个MessageBox,查阅过相关文档,都没找到手动关闭的方法;我是这样调用的:

Get(url, params, callback) {
    axios.get(url, {
      params
    }).then(res => {
      if (res.data.ret === 0) {
        callback(res);
      } else if (res.data.ret === 7) { //登录超时
        MessageBox({
          title: '系统提示',
          message: res.data.msg,
          confirmButtonText: '确定',
          closeOnPressEscape: false,
          showCancelButton: false
        }).then(() => {
          router.push('login');
        }).catch(() => {
          router.push('login');
        });
      } else {
        Message({
          message: res.data.msg,
          type: 'error',
          duration: 5 * 1000
        });
      }
    }).catch(err => {
      Message({
        message: err.message,
        type: 'error',
        duration: 5 * 1000
      });
    });
  }
阅读 24.2k
5 个回答

我仔细看了element-ui的文档http://element.eleme.io/#/zh-...,看到了这个例子https://jsfiddle.net/api/post...,文档里有这样一段话,图片描述

就是这个done方法,最终解决了困扰我多时的问题,只要修改成这样,就能关掉那些重复弹出的MessageBox了:

MessageBox({
    title: '系统提示',
    message: res.data.msg,
    confirmButtonText: '确定',
    closeOnPressEscape: false,
    showCancelButton: false,
    beforeClose: (action, instance, done) => {
        done(); // 就是它!
    }
}).then(()=>{
    //doSomething();
}).catch(()=>{
    //doSomething2();
});

看文档的时候真是需要多琢磨啊,也多谢楼上的回答;

新手上路,请多包涵

我的做法是,第一次弹出messagebox,记录一个标志位,以后的弹窗就是不处理,退到登陆界面之后,消除标志位

新手上路,请多包涵

主动关闭element UI message box弹出层:
clipboard.png
class="v-modal"是遮罩层,在你所需的事件中 $('.v-modal').click();
就可以主动关闭弹出层及遮罩层。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题