网站首页 > 技术文章 正文
路由导航守卫
router.beforeEach
router.afterEach
router.beforeResolve
beforeEnter
beforeRouteEnter
beforeRouteUpdate (2.2 新增)
beforeRouteLeave
1.全局前置守卫----router.beforeEach
router.beforeEach 注册一个全局前置守卫:
const router = new VueRouter({ ... })
router.beforeEach((to, from, next) => {
// to:要去哪个页面
// from:从哪里来
// next:它是一个函数。
// 如果直接放行 next()
// 如果要跳到其它页 next(其它页)
})
示例代码:
router.beforeEach(async(to, from, next) => {
NProgress.start() // 开启进度条
const token = store.state.user.token
const userId = store.state.user.userInfo.userId
console.log(token, to.path, from.path)
if (token) {
if (to.path === '/login') { // 有 token还要去login
next('/')
NProgress.done() // 关闭进度条
} else { // 有 token,去其他页面,放行
if (!userId) {
await store.dispatch('user/getUserInfo')
}
next()
}
} else {
if (to.path === '/login') { // 没有token,去login,放行
next()
} else {
next('/login') // 没有token,去其他页面
NProgress.done()
}
}
})
小结
- router.beforeEach(回调(三个参数))
- 导航守卫函数中,一定要调用next( )
- to.path: to是一个路由对象,path表示路径,是它的一个属性
2. 全局后置钩子----router.afterEach
router.afterEach 注册一个全局后置钩子:
你也可以注册全局后置钩子,然而和守卫不同的是,这些钩子不会接受 next 函数也不会改变导航本身:
router.afterEach((to, from) => {
// ...
})
3. 全局解析守卫----router.beforeResolve
在 2.5.0+ 你可以用 router.beforeResolve 注册一个全局守卫。这和 router.beforeEach 类似,区别是在导航被确认之前,同时在所有组件内守卫和异步路由组件被解析之后,解析守卫就被调用。
4.路由独享的守卫
你可以在路由配置上直接定义 beforeEnter 守卫:
const router = new VueRouter({
routes: [
{
path: '/foo',
component: Foo,
beforeEnter: (to, from, next) => {
// ...
}
}
]
})
5.组件内的守卫
- beforeRouteEnter
- beforeRouteUpdate (2.2 新增)
- beforeRouteLeave
const Foo = {
template: `...`,
beforeRouteEnter(to, from, next) {
// 在渲染该组件的对应路由被 confirm 前调用
// 不!能!获取组件实例 `this`
// 因为当守卫执行前,组件实例还没被创建
},
beforeRouteUpdate(to, from, next) {
// 在当前路由改变,但是该组件被复用时调用
// 举例来说,对于一个带有动态参数的路径 /foo/:id,在 /foo/1 和 /foo/2 之间跳转的时候,
// 由于会渲染同样的 Foo 组件,因此组件实例会被复用。而这个钩子就会在这个情况下被调用。
// 可以访问组件实例 `this`
},
beforeRouteLeave(to, from, next) {
// 导航离开该组件的对应路由时调用
// 可以访问组件实例 `this`
}
}
完整的导航解析流程
- 导航被触发。
- 在失活的组件里调用 beforeRouteLeave 守卫。
- 调用全局的 beforeEach 守卫。
- 在重用的组件里调用 beforeRouteUpdate 守卫 (2.2+)。
- 在路由配置里调用 beforeEnter。
- 解析异步路由组件。
- 在被激活的组件里调用 beforeRouteEnter。
- 调用全局的 beforeResolve 守卫 (2.5+)。
- 导航被确认。
- 调用全局的 afterEach 钩子。
- 触发 DOM 更新。
- 调用 beforeRouteEnter 守卫中传给 next 的回调函数,创建好的组件实例会作为回调函数的参数传入。
猜你喜欢
- 2025-06-10 下一代 Vue3 Devtools 正式开源!(vue3正式版发布)
- 2025-06-10 HyperExpress 来了,老牌 Express 要下线?
- 2025-06-10 大厂面试必问:如何设计一个扛高并发的系统?
- 2025-06-10 初学vue3, 全是黑盒子,vue3知识点汇总
- 2025-06-10 62K Star!Syncthing,打造你的专属局域网文件同步利器!
- 2025-06-10 Web 前端思考题:如何获取往返数据包的 TTL
- 2024-09-30 Vue Router 4 路由地址详解 vue router路由配置
- 2024-09-30 「vue基础」一篇浅显易懂的 Vue 路由使用指南( Vue Router 下)
- 2024-09-30 Vue Router 4 路由操作 - 路由导航
- 2024-09-30 为什么用vue.js,为什么前端开发46%的人都在用?
你 发表评论:
欢迎- 06-24发现一款开源宝藏级工作流低代码快速开发平台
- 06-24程序员危险了,这是一个 无代码平台+AI+code做项目的案例
- 06-24一款全新的工作流,低代码快速开发平台
- 06-24如何用好AI,改造自己的设计工作流?
- 06-24濮阳网站开发(濮阳网站建设)
- 06-24AI 如何重塑前端开发,我们该如何适应
- 06-24应届生靠这个Java简历模板拿下了5个offer
- 06-24服务端性能测试实战3-性能测试脚本开发
- 565℃Oracle分析函数之Lag和Lead()使用
- 562℃几个Oracle空值处理函数 oracle处理null值的函数
- 548℃Oracle数据库的单、多行函数 oracle执行多个sql语句
- 545℃0497-如何将Kerberos的CDH6.1从Oracle JDK 1.8迁移至OpenJDK 1.8
- 541℃Oracle 12c PDB迁移(一) oracle迁移到oceanbase
- 535℃【数据统计分析】详解Oracle分组函数之CUBE
- 526℃最佳实践 | 提效 47 倍,制造业生产 Oracle 迁移替换
- 517℃Oracle有哪些常见的函数? oracle中常用的函数
- 最近发表
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端react (48)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端富文本编辑器 (47)
- 前端路由 (61)
- 前端数组 (73)
- 前端js面试题 (50)
- 前端定时器 (59)
- Oracle RAC (73)
- oracle恢复 (76)
- oracle 删除表 (48)
- oracle 用户名 (74)
- oracle 工具 (55)
- oracle 内存 (50)
- oracle 导出表 (57)
- oracle 中文 (51)
- oracle的函数 (57)
- 前端调试 (52)
- 前端登录页面 (48)
本文暂时没有评论,来添加一个吧(●'◡'●)