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()
}
相关推荐
用户63879947730518 小时前
每组件(Per-Component)与集中式(Centralized)i18n
前端·javascript
SsunmdayKT18 小时前
React + Ts eslint配置
前端
开始学java18 小时前
useEffect 空依赖 + 定时器 = 闭包陷阱?count 永远停在 1 的坑我踩透了
前端
zerosrat18 小时前
从零实现 React Native(2): 跨平台支持
前端·react native
狗哥哥18 小时前
🔥 Vue 3 项目深度优化之旅:从 787KB 到极致性能
前端·vue.js
青莲84318 小时前
RecyclerView 完全指南
android·前端·面试
青莲84318 小时前
Android WebView 混合开发完整指南
android·前端·面试
GIS之路18 小时前
GDAL 实现矢量数据转换处理(全)
前端
大厂技术总监下海18 小时前
Rust的“一发逆转弹”:Dioxus 如何用一套代码横扫 Web、桌面、移动与后端?
前端·rust·开源
加洛斯18 小时前
SpringSecurity入门篇(2):替换登录页与config配置
前端·后端