get route params in asyncData after router push

蓝咒 提交于 2021-01-03 06:57:42

问题


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.

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` })
}

回答1:


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



回答2:


I'm doing it in a similar way but I retrieve params and almost everything from context.

async asyncData(context) {
        const plans = await context.$axios.get("business/get-plans", {
            params: {
                currency: context.query.currency
            }
        }).then((res) => {
            return res.data;
        });

Take a look to context.$axios and context.query.



来源:https://stackoverflow.com/questions/54992618/get-route-params-in-asyncdata-after-router-push

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!