Appearance
next() 作用
本节目标:理解“next() 作用”的核心思路,并能把它用于实际开发或面试表达。 在 Vue Router 的
beforeEach导航守卫中,next函数用于控制导航的流程。它的调用方式有多种,可以传入不同的参数来执行不同的导航行为。next函数的调用方法和参数
- next()调用后,不会再进入beforeEach;
- next(arg)带参数的调用,会再次进入beforeEach,处理不好会死循环;
next()无参数调用next,表示允许导航继续。 router.beforeEach((to, from, next) => { next(); // 允许导航 });next(false)传入false来取消当前导航。如果浏览器的 URL 已经改变(通常是用户手动输入 URL 或点击浏览器的前进/后退按钮),当取消导航后,URL 会重置到from路由对应的地址。 router.beforeEach((to, from, next) => { next(false); // 取消导航 });next('/')或next({ path: '/' })传入一个字符串路径或者一个描述目标位置的对象,来重定向导航。 router.beforeEach((to, from, next) => { next('/'); // 重定向到首页 // 或者 next({ path: '/' }); });next({ path: '/', replace: true })使用replace选项来重定向并且不会在浏览器历史记录中留下记录。 router.beforeEach((to, from, next) => { next({ path: '/', replace: true }); // 重定向到首页,不留下历史记录 }); ==next()== 会跳出 ==beforeEach==, 并且跳转到我们想去的组件,但是 ==next({ ...to, replace: true });== 不会,它会再次进入 ==beforeEach== 这个函数, 然后 就会陷入死循环.next({ name: 'home' })传入一个命名路由对象来重定向导航。 router.beforeEach((to, from, next) => { next({ name: 'home' }); // 重定向到名为 'home' 的路由 });next(new Error('navigation error'))传入一个Error实例,导航会被终止,同时会调用router.onError注册的回调。 router.beforeEach((to, from, next) => { next(new Error('navigation error')); // 终止导航并触发错误 });
总结
next():继续导航next(false):取消导航next('/')或next({ path: '/' }):重定向到指定路径next({ path: '/', replace: true }):重定向并替换当前记录next({ name: 'home' }):重定向到命名路由next(new Error('navigation error')):抛出导航错误 通过这些调用方式,你可以灵活控制 Vue Router 中的导航行为。
