问题
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