Skip to content

next() 作用 ​

本节目标:理解“next() 作用”的核心思路,并能把它用于实际开发或面试表达。 在 Vue Router 的 beforeEach 导航守卫中,next 函数用于控制导航的流程。它的调用方式有多种,可以传入不同的参数来执行不同的导航行为。 next 函数的调用方法和参数

  • next()调用后,不会再进入beforeEach;
  • next(arg)带参数的调用,会再次进入beforeEach,处理不好会死循环;
  1. next() 无参数调用 next,表示允许导航继续。 router.beforeEach((to, from, next) => { next(); // 允许导航 });

  2. next(false) 传入 false 来取消当前导航。如果浏览器的 URL 已经改变(通常是用户手动输入 URL 或点击浏览器的前进/后退按钮),当取消导航后,URL 会重置到 from 路由对应的地址。 router.beforeEach((to, from, next) => { next(false); // 取消导航 });

  3. next('/') 或 next({ path: '/' }) 传入一个字符串路径或者一个描述目标位置的对象,来重定向导航。 router.beforeEach((to, from, next) => { next('/'); // 重定向到首页 // 或者 next({ path: '/' }); });

  4. next({ path: '/', replace: true }) 使用 replace 选项来重定向并且不会在浏览器历史记录中留下记录。 router.beforeEach((to, from, next) => { next({ path: '/', replace: true }); // 重定向到首页,不留下历史记录 }); ==next()== 会跳出 ==beforeEach==, 并且跳转到我们想去的组件,但是 ==next({ ...to, replace: true });== 不会,它会再次进入 ==beforeEach== 这个函数, 然后 就会陷入死循环.

  5. next({ name: 'home' }) 传入一个命名路由对象来重定向导航。 router.beforeEach((to, from, next) => { next({ name: 'home' }); // 重定向到名为 'home' 的路由 });

  6. 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 中的导航行为。

本站总访问 — 次