Getting the query params from a URL in Nuxt

Getting the query params from a URL in Nuxt

In this post we will give you information about Getting the query params from a URL in Nuxt. Hear we will give you detail about Getting the query params from a URL in NuxtAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to get the query params from a URL in the nuxt app.

Query params

Query params are passed to the end of a URL using a question mark (?) followed bythe (key=value) pairs.

Consider, we have the following URL with the query parameter in our nuxt app.

localhost:3000/persons?name=john

Getting the query params

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

Persons.vue
&lt;template&gt;  &lt;div&gt;    &lt;h1&gt;This is the Persons page&lt;/h1&gt;  &lt;/div&gt;&lt;/template&gt;&lt;script&gt;export default {  created() {    console.log(this.$route.query.name); // john  },};&lt;/script&gt;

Inside the vue component template, you can access it like this.

Persons.vue
&lt;template&gt;  &lt;div&gt;    &lt;h1&gt;This is the Persons page&lt;/h1&gt;    &lt;p&gt;{{$route.query.name}}&lt;/p&gt;  &lt;/div&gt;&lt;/template&gt;&lt;script&gt;export default {  created() {    console.log(this.$route.query.name); // john  },};&lt;/script&gt;

If you are passing multiple query parameters to a URL using & operator.

localhost:3000/persons?name=john&amp;id=1

You can access it like this.

Persons.vue
&lt;template&gt;  &lt;div&gt;    &lt;h1&gt;This is the Persons page&lt;/h1&gt;    &lt;p&gt;Name: {{$route.query.name}}&lt;/h1&gt;    &lt;p&gt;Id: {{$route.query.id}}&lt;/h1&gt;  &lt;/div&gt;&lt;/template&gt;

Hope this code and post will helped you for implement Getting the query params from a URL in Nuxt. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US