by Sai gowtham

How to get url params from a route in Nuxt

In this tutorial, we are going to learn about how to get the URL params from a current route in Nuxt.

Consider we have this following dynamic path with a parameter id.

pages/users/_id.vue

Getting the url params

To get the url param from a route, we can use this.$route.params object inside the vue component <script> tag.

Example:

_id.vue
<script>
export default{
   data: function() {
    return {
        // id is name of the url param we created
      userId: this.$route.params.id
    };
  }
}
</script>

Inside the vue component template, we can access it without using this keyword.

_id.vue
<template>
  <div>
    <p>User id - {{$route.params.id}}</p>  </div>
</template>

Now, if we navigate to localhost:3000/users/1 we’ll see id 1 is rendered on the screen.

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course