Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass the id number in the link using nuxt-link

I'm working on small project using nuxt.js and i would like to create a link to my products varints using <nuxt-link to=""> and i don't know how can i pass the id of the product that i get from the database i tried with but i get an error.

what should i do ? this is my code :

<tr v-for="product in variables.laravelData.data" :key="product.id">
              <td>
                <input type="checkbox" name="">
              </td>
              <td><img src="images/ui/thumb.jpg" width="38" height="38" alt=""></td>
              <td>
                <nuxt-link to="/products/variants/{{product.id}}">
                  {{ product.product_name }}
                </nuxt-link>
              </td>
</tr>
like image 490
Ricardo Abel Avatar asked Jan 26 '20 21:01

Ricardo Abel


People also ask

How do I style a NUXT link?

The only thing you have to do to is add the nuxt-link-exact-active class to your styles and then you can style it as you wish. You can add this css to your navigation component or to a specific page or layout or in your global css file. See the nuxt docs for more information on adding global css files.

How does NUXT link work?

Nuxt automatically includes smart prefetching. That means it detects when a link is visible, either in the viewport or when scrolling and prefetches the JavaScript for those pages so that they are ready when the user clicks the link.

How do I get the current route name in NUXT?

To get the current route path in nuxt, we can use this. $route. path or $this.

How do I get query params in NUXT?

To get the query params from an above URL, we can use this. $route. query object inside our vue component <script> tag. Inside the vue component template, you can access it like this.


2 Answers

You should to bind the to directive to write a dynamic id using template strings, as below:

<nuxt-link :to="`/products/variants/${product.id}`">
like image 187
Christian Carrillo Avatar answered Sep 24 '22 12:09

Christian Carrillo


Does this work?

<nuxt-link :to="{name: 'name-of-your-router', params: { id: product.id } }">

According to this https://github.com/nuxt/nuxt.js/issues/1546#issuecomment-326267738 :

If you want to use params you need to use name and not path:

<nuxt-link :to="{name: 'post-detail-id', params: { id:idPost } }">{{title}}</nuxt-link>

You can see the name for each route into .nuxt/router.js

like image 23
webprogrammer Avatar answered Sep 21 '22 12:09

webprogrammer