vue2 组件内路由守卫使用

1、beforeRouteEnter 进入页面

to -- 即将要跳转到的页面

form -- 跳转前的页面,从哪个页面跳转过来的

next -- 下一步,若无指定跳转的路由,设置为空 next() 即可

复制代码
beforeRouteEnter(to, from, next) {
    next()
},

使用 beforeRouteEnter 时,组件还没有创建,因此无法使用 this,当我们需要写逻辑的时候可以在 next() 里面写,具体如下:

复制代码
beforeRouteEnter(to, from, next) {
  next(vm => {
    if (from.path === '/details') {
      vm.getData('1');
    } else {
      vm.getData();
    }
  })
}

2、beforeRouteLeave 离开页面

使用方法和 beforeRouteEnter 一样,同样有 to,from,next

复制代码
beforeRouteLeave(to, from, next) {
    next()
}
相关推荐
JarvanMo12 小时前
Fluwx 6.0 预览版本他来了
前端
KaMeidebaby12 小时前
卡梅德生物技术快报|单 B 细胞抗体筛选服务:技术架构、流程实现与数据验证
前端·数据库·其他·百度·新浪微博
爱勇宝12 小时前
别焦虑,也别躺平:给年轻程序员的一封信
前端·后端·架构
OpenTiny社区12 小时前
2026 OpenTiny NEXT 产品调研启动!
前端·开源·github
德莱厄斯12 小时前
GIS 开发要变天?看看高德空间智能给我们带来了什么!
前端·gis·agent
JarvanMo12 小时前
Flutter.Dart的主构造函数(primary constructor)可能马上就要来了
前端
用户658681803384013 小时前
一个前端CLAUDE.md
前端
前端那点事13 小时前
彻底解决KeepAlive缓存乱象!Vue3精细化按需缓存+路径重置终极方案
前端·vue.js
前端那点事13 小时前
Vue 的 template 标签不能用 v-show?底层机制+踩坑复盘+生产级解决方案
前端·vue.js
前端那点事13 小时前
从零落地前端性能优化:全链路避坑+实战调优方案
前端·vue.js