by Sai gowtham

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>

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