My nuxtjs app contains page with dynamic url (actually page has two params event-slug
and event-id
), then in that page's asyncData
method, I extract these params from URL and use for axios
request. This page opens when I click on one item on list of events (on another events-list
page). When I trigger nuxt generate
command, I encounter error because these params are not available while generating static pages.
Is there any solution?
SOLUTION (in case someone needs it later):
Assume that I have /event/:slug/:id
dynamic route in my Nuxt app and need to create route for all events I receive from backend, add this code into nuxt.config.js
:
generate: {
routes: () => {
axios.get('endpoint to get list of events')
.then((response) => {
// assume we received array of events in response.data
let events = response.data;
return events.map((event) => {
// assume that my Nuxt dynamic route is /event/:slug/:id
return `/event/${event.slug}/${event.id}`
})
})
}
},