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:
<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.
<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.