Params字段在$ router.push中是空的

Joz*_*ipa 14 url-parameters vue.js vue-resource vue-router

我用这个

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

在我的组件中,当发生一些错误时重定向到其他URL.问题是,当我想访问params仪表板组件中的字段时,它是空的.该query领域运作良好.我正试图通过它来访问它this.$route.params.errors.

euv*_*uvl 34

您可以使用paramsnamed路径(我认为).

例:

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

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

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

  • 悲剧的限制。 (3认同)
  • 你知道这个限制的原因是什么吗? (2认同)

Rob*_*Rob 5

如果你希望使用命名路由你可以试试这个:

ES6

this.$root.$router.push({
    path: `/dashboard/${error}`, 
    query: { test }
})
Run Code Online (Sandbox Code Playgroud)

ES5

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