路由器推送后获取 asyncData 中的路由参数

Kin*_*Net 3 vue-router vuejs2 nuxt.js

我试图在 asyncData 方法中获取路由参数,如果我手动转到路由,它会起作用,但是如果我使用路由器 Push 方法,它不会得到任何参数。

这是我的 asynData 方法:

  async asyncData(ctx) {
    const { id } = ctx.app.router.currentRoute.params
    await ctx.store.dispatch('user/GET_USER', id)
    return {
      user: ctx.store.state.user.user
    }
  },
Run Code Online (Sandbox Code Playgroud)

这就是我导航到相应页面的方式:

goToEdit(id) {
  this.$router.push({ path: `/users/${id}/edit` })
}
Run Code Online (Sandbox Code Playgroud)

Ald*_*und 9

您需要直接从 ctx 获取路由。是上下文参数列表

 async asyncData({ route }) {
    const { id } = route.params
    await ctx.store.dispatch('user/GET_USER', id)
    return {
      user: ctx.store.state.user.user
    }
},
Run Code Online (Sandbox Code Playgroud)