javascript - vue-router - 使用新导航从 "/"取消到 "/password"的导航

标签 javascript vue.js vue-router

尝试导航到密码页面,vue-router 抛出此错误

Uncaught (in promise) Error: Navigation cancelled from "/" to "/password" with a new navigation.
导航到密码页面已成功重定向,但不确定抛出此错误的原因。我之前没有观察到这个错误。
Vue路由器版本:
vue-router@3.3.4
单击按钮执行 this.$router.push({name: 'password', query: {username: this.username}})在路由器/index.js
{
path: '/',
component: loginComponent,
children: [
  {
    path: '/',
    alias: '/login',
    name: 'login',
    component: () => import('../views/email.vue')
  },
  {
    path: 'password',
    name: 'password',
    component: () => import('../views/password.vue')
  }
]
}

最佳答案

我最近遇到了同样的错误。发生这种情况是因为 vue 路由器无法重定向两次(这意味着您无法在重定向的路由中重定向)。这在 documentation 中有所提及

Make sure that the next function is called exactly once in any given pass through the navigation guard. It can appear more than once, but only if the logical paths have no overlap, otherwise the hook will never be resolved or produce errors.


我有一个解决方法是创建一个隐藏的 vue-route具有要重定向到的路由的组件,然后单击。
例子:
<router-link ref="redirect" :to="$route.query.redirect ? $route.query.redirect : '/'"></router-link>
并且当您想要重定向时(可能是在用户输入密码后的情况),您可以在此链接上触发点击事件
this.$refs.redirect.$el.click()

关于javascript - vue-router - 使用新导航从 "/"取消到 "/password"的导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65284853/

相关文章:

vue.js - vuejs 路由器 - this.$route 在刷新时始终为空

javascript - 通过 Angular Directive(指令)访问 watch 内的变量

javascript - 黑客保留 history.pushState ie8-9

javascript - 找不到模块 'meteor'

javascript - 如何在 HTML 和 Javascript 中实现基本计算器

javascript - Vue.JS + webpack 构建事件发射问题

vue.js - 设置 Vuetify 应用栏扩展的颜色

javascript - Vue.js 2/Javascript mouseover 方法在 mousein 和 mouseout 上应用事件

vue.js - 如何更改node_modules中Vue的src代码进行测试

javascript - 在桌面上的单个 View 中显示多个路由器组件