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()
}
相关推荐
李伟_Li慢慢7 小时前
14-mini版WebGLPrograms
前端·three.js
李伟_Li慢慢7 小时前
19-mini版WebGLRenderer
前端·three.js
李伟_Li慢慢7 小时前
10-mini版bindingStates
前端·three.js
এ慕ོ冬℘゜7 小时前
深入 JavaScript BOM:掌握 window 对象的窗口控制魔法
开发语言·javascript·ecmascript
李伟_Li慢慢7 小时前
08-BufferAttribute
前端·three.js
李伟_Li慢慢7 小时前
09-mini版WebGLAttributes
前端·three.js
李伟_Li慢慢7 小时前
11-mini版WebGLGeometries
前端·three.js
李伟_Li慢慢7 小时前
18-mini版WebGLRenderList
前端·three.js
李伟_Li慢慢7 小时前
15-mini版WebGLMaterials
前端·three.js
ji_shuke7 小时前
Vue3 前端批量打印 PDF/图片踩坑记:跨域、合并打印、对话框闪退与按钮一直 loading
前端·pdf·状态模式·pdf打印