路由器推送后获取 asyncData 中的路由参数 [英] get route params in asyncData after router push

查看:221
本文介绍了路由器推送后获取 asyncData 中的路由参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get the route params in the asyncData method, it works if I go manually to the route, but if I use router Push method, it doesn't get any params.

这是我的 asynData 方法:

this is my asynData method:

  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
    }
  },

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

and this is how I navigate to the respective page:

goToEdit(id) {
  this.$router.push({ path: `/users/${id}/edit` })
}

推荐答案

需要直接从 ctx 获取路由.此处上下文参数列表

You need to get route from ctx directly. Here a list of context arguments

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

这篇关于路由器推送后获取 asyncData 中的路由参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆