$router.push 中的 Params 字段为空

新手上路,请多包涵

考虑一下:

 this.$root.$router.push({
    path: '/dashboard',
    params: { errors: 'error' },
    query: { test: 'test' }
})

我在我的组件中使用它来重定向到另一个 URL,并且发生了一些错误。问题是当我想访问仪表板组件中的 params 字段时,它是空的。 query 字段运行良好。我正在尝试通过 this.$route.params.errors 访问它。

原文由 Jozef Cipa 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
2 个回答

您只能使用 paramsnamed 路径(我认为)。

例子:

 //route (in your router file should have "name")
{ path: '/errors', name: 'EXAMPLE', component: ... }

//navigating
this.$router.push({
    name: 'EXAMPLE',
    params: { errors: '123' }
});

现在它将在 this.$route.params 中具有正确的值。

原文由 euvl 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果你 不想 使用 _命名路由_,你可以试试这个:

ES6

 this.$root.$router.push({
    path: `/dashboard/${error}`,
    query: { test }
})

ES5

 this.$root.$router.push({
    path: '/dashboard/' + error,
    query: { test: 'test' }
})

原文由 Rob 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题