vue中如何拦截阻断element-ui tab组件切换

首先我们来看下element-ui  tab组件中的描述

那么就是说在before-leave这个回调函数触发之后,必须给这个方法一个返回值,返回false则会阻止tab页面跳转,否则发生跳转

我的业务场景:用户切换到当前tab页之后会对当前页面数据做操作,操作之后如果不点击保存,跳转tab页,则提示用户“当前页面未保存”,否则成功跳转,如图效果

代码如下,看官网对confirm是如何描述的,“我们将用Promise来处理后续响应”,也就是说,在点击了确定之后,用户会走到then方法中,这里时候是promise对象,允许用户跳转tab,如果是catch,我们手动抛出异常,中断用户跳转

    beforeLeave(newname,oldename){
      if(oldename =='fourth'){
        if(this.$store.state.editData && !this.$store.state.saveFlag){
          return this.$confirm('系统将不会保留您所做的更改!, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              // this.$store.commit('changeEditData',false)
              // this.$store.commit('changeSaveFlag',true)
              this.clickTab(newname)
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消保存'
              });

              throw new Error("取消成功!");
            });
        }else{
          this.activeTab = newname;
          this.clickTab(newname)
        }
      } else {
        this.activeTab = newname;
        if(newname != 'fourth'){
          this.clickTab(newname)
        }
      }
    },

用户未保存,阻断路由切换

  beforeRouteLeave (to, from, next) {
    if (this.$store.state.editData && !this.$store.state.saveFlag) {
      return this.$confirm('系统将不会保留您所做的更改!', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        next()
      }).catch(err => {
        this.$message({
          type: 'info',
          message: '已取消保存'
        }); 
        next(false)
      })  
    } else {
      next()
    }
  },

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值