Home > Software design >  Getting data with dynamic routing
Getting data with dynamic routing

Time:12-24

I have a catalog item, by clicking I generate a slug and insert it into the NuxtLink.

Now in the component I need to get a specific element from the array, but I don't have a slug in the array. With the help of id, I would be able to find the desired element. I don't provide any id to Nuxt routing.

If I can somehow get the id of the desired element, then how do I do it?

I think it would be a bad idea to record the id in the pinia store.

How do they act in such situations in general? I use nuxt 3.

<NuxtLink  :to="`catalog/${slug}`"></NuxtLink>

CodePudding user response:

You can use the params property to pass the id of the desired element as a route parameter.

<NuxtLink  :to="`catalog/${slug}/${id}`"></NuxtLink>

Then, in the component that is rendered when the NuxtLink is clicked, you can access the id route parameter using the $route.params.id property.

mounted() {
  const id = this.$route.params.id
  // use the id to get the desired element from the array
}
  • Related