Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vue-router 3.1.0+ 版本 跳转到当前路径会报错 #151

Open
lovelmh13 opened this issue Jul 26, 2021 · 0 comments
Open

vue-router 3.1.0+ 版本 跳转到当前路径会报错 #151

lovelmh13 opened this issue Jul 26, 2021 · 0 comments

Comments

@lovelmh13
Copy link
Owner

原因

  1. push 中调用的 confirmTransition函数
    image

image

  1. confirmTransition函数会判断要添加进队列的路由是不是与前一个路由是同一个路由( isSameRoute 函数),isSameRoute中又会判断路由的参数是不是同一个对象( isObjectEqual 函数 )

image

  1. 如果是,则会抛出一个 创建重复导航的错误( createNavigationDuplicatedError )

解决方法

利用 AOP 切片,修改push方法

// 利用 AOP 解决 vue router 3.1.0+ 相同路由报错的问题
Router.prototype.push = function push (location, onResolve, onReject) {
  // 这一段是 Router.prototype.push 的源码,当跳转路由有还有要执行的cb时,执行这里
  if (onResolve || onReject) {
    return originalRouterPush.call(this, location, onResolve, onReject)
 }
  // 接收住路由的报错,不显示
  return originalRouterPush
   .call(this, location)
   .catch((error) => error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant