vue 表单编辑中退出页面提示

1.编辑页代码(需要使用该功能的页面)

beforeUpdate() {
    // 重置表单
    const { useStaff, fuzzyKeywords, accurateKeywords, ruleForm, sceneList }=this.$options.data();
    sceneList.push({ chatRoomList: [], tagList: [] });
    const oldVal = { //存到变量oldVal(初始数据)中
      useStaff,
      fuzzyKeywords,
      accurateKeywords,
      sceneList:
        this.labelType === AUTOLABEL_TYPE["intoGroup"]
          ? [{ chatRoomList: [], tagList: [] }]
          : [
              {
                sceneType: NEWCUSOMTER_SCENE_TYPE["day"],
                loopPoint: NEWCUSOMTER_SCENE_TYPE["day"],
              },
            ],
      ...ruleForm,
    };
    const newVal = { //变量newVal(实时更改的数据)
      useStaff: this.useStaff,
      fuzzyKeywords: this.fuzzyKeywords,
      accurateKeywords: this.accurateKeywords,
      sceneList: this.sceneList,
      ...this.ruleForm,
    };
    checkChange(oldVal, newVal);  // 调用的utils中的方法
  },

2.utils中的checkChange方法(checkChange对应上图的checkChange,是从utils中导入的)

 export const checkChange = (oldVal, newVal) => {
   // 将新值与旧值进行对比并取反,存入sessionStorage的change中
  window.sessionStorage.setItem('change', !isEqual(newVal, oldVal));
};

3.在main.js中设置路由导航守卫监听

router.beforeEach((to, from, next) => {
  // 拦截需要提示的路由name,whitePathName:这里相当于一个白名单,用于判断哪些页面需要使用该功能
  const whitePathName = ['CustomerOSP/addSOP',"AddRule"]; 
  //所有的编辑页
  if (from.path !== '/' && !whitePathName.includes(from.name)) {
    // 设置编辑页从哪个路由跳转来,在返回的时候使用push
    window.sessionStorage.setItem('from', from.path); 
  }
  // 若当前是通过浏览器返回,则不进行拦截
  if (window.history && window.history.pushState) {
    let url = window.location.pathname + window.location.search
    if (url !== from.fullPath) {
      return next();
    }
  }
  if (!whitePathName.includes(from.name)) return next();
  if (window.sessionStorage.getItem('change') === 'true') {
    Vue.prototype.$confirm('离开后,当前页面更改内容不会保存,是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      next();
      window.sessionStorage.setItem('change', false);
    }).catch(() => {
      next(false);
    });
  } else {
    next();
  }
});

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值