VUE路由导航,(路由拦截 登录、权限、跳转限制)。VUE生命周期:beforeRouteLeave

本文介绍了如何在Vue路由守卫中实现用户编辑内容离开页面时的保存提示,重点讲解了`beforeRouteLeave`生命周期钩子的应用,并解决了用户点击取消导致的死循环问题。通过实例代码展示了如何结合Element UI的MessageBox进行交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求:用户编辑后,若池中有数据,则在离开页面时提示保存。
由于一个页面不单会有返回,还会有其他各个页面的导航,导致单把逻辑写在返回按钮中无法实现需求。路由导航可以做到:
官方文档
参考

router.beforeEach((to, from, next) => {
  console.log(to);   // 即将要进入路由的对象
  console.log(from); // 当前导航要离开的路由对象
  console.log(next); // 调用next该方法,才能进入下一个路由
  next();
});

实例代码:router根文件


import {MessageBox} from 'element-ui'
import store from '../store/index'
import Router from 'vue-router'
//..............
const router = new Router({......})
//..............
router.beforeEach((to, from, next) => {
  let fromPath = from.fullPath==='/vocabulary/create'||from.fullPath==='/vocabulary/edit'
  let toPath = to.fullPath!='/vocabulary/create'&&to.fullPath!='/vocabulary/edit'
  let editTF = store.state.vocabulary.checkedList.length;
  // 来自这俩路由 去的不是这俩路由 且有编辑内容
  if(editTF&&fromPath&&toPath){
      MessageBox.confirm('您有编辑内容尚未保存,确认退出?', '提示', {
          confirmButtonText: '确定',
          type: 'warning',
          center: true,
      }).then(() => {
          store.commit(`vocabulary/coverCheckedList`,[]);
          next();
      }).catch(() => {
//         // next(false);
//         // router.replace(to.fullPath);
//         // router.push(to.fullPath)
//           // next(from.fullPath);
//           // abort();
        // router.back();
      });
    } else {
      store.commit(`vocabulary/setSearchModel`,{});
      store.commit(`vocabulary/setSearchModel_Page`,{});
      store.commit(`vocabulary/setCurTab`,true);
      next();
    } 
 })

但由于我需求具有一定特殊性,这样会导致用户点击取消进入死循环,试了很多方法都不行。
于是另辟蹊径,找到VUE的生命周期:beforeRouteLeave

    beforeRouteLeave(to,from,next){
        let fromPath = from.fullPath==='/vocabulary/create'||from.fullPath==='/vocabulary/edit'
        let toPath = to.fullPath!='/vocabulary/create'&&to.fullPath!='/vocabulary/edit'
        let editTF = this.$store.state.vocabulary.checkedList.length;
        // 来自这俩路由 去的不是这俩路由 且有编辑内容
        if(editTF&&fromPath&&toPath){
            this.$confirm('您有编辑内容尚未保存,确认退出?', '提示', {
                confirmButtonText: '确定',
                type: 'warning',
                center: true,
            }).then(() => {
                this.$store.commit(`vocabulary/coverCheckedList`,[]);
                next();
            }).catch(() => {
                return
            });
        } else {
            this.$store.commit(`vocabulary/setSearchModel`,{});
            this.$store.commit(`vocabulary/setSearchModel_Page`,{});
            this.$store.commit(`vocabulary/setCurTab`,true);
            next();
        } 
    }

完美解决需求~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

跳动的世界线

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值