Getting the current route path (name) in Nuxt
In this tutorial, we are going to learn about how to get the current route path from a URL in Nuxt.
Consider we are in this following path.
localhost:3000/users
To get the current route path in nuxt, we can use this.$route.path
or $this.$nuxt.$route.path
inside the vue component <script>
tag.
this.$route.path
property returns the absolute path from a URL as a string format.
Example:
<script>
export default{
created(){
console.log(this.$route.path); // path is /users
}
}
</script>
Inside the vue component template, we can access it without using the this
keyword.
<template>
<div>
<p>{{$route.path}}</p> </div>
</template>
You can also get the current route name like this.
<template>
<div>
<p>{{$route.name}}</p> </div>
</template>