How to get current name of route in Vue?

Viewed 180554

I want to get the name of the current route of vue-router, i have a component menu with navigation to another componentes, so i want to dispaly the name of the current route. I have this:

created(){
    this.currentRoute;
    //this.nombreRuta = this.$route.name;
},
computed:{
    currentRoute:{
        get(){
            this.nombreRuta = this.$route.name;
        }
    }
}

But the label of the name of the route does not change, the label only show the name of the first loaded route. Thank You

EDIT:

enter image description here

Image to show what i want

13 Answers

You are using computed incorrectly. You should return the property in the function. See the docs for more information.

Here is your adapted example:

computed: {
    currentRouteName() {
        return this.$route.name;
    }
}

You can then use it like this:

<div>{{ currentRouteName }}</div>

You can also use it directly in the template without using a computed property, like this:

<div>{{ $route.name }}</div>

Vue 3 + Vue Router 4

Update 5/03/2021

If you are using Vue 3 and Vue Router 4, here is two simplest ways to get current name of route in setup hook:

Solution 1: Use useRoute

import { useRoute } from 'vue-router';
export default {
  setup () {
    const currentRoute = computed(() => {
      return useRoute().name
    })
    return { currentRoute }
  }
}

Solution 2: Use useRouter

import { useRouter } from 'vue-router';
export default {
  setup () {
    const currentRoute = computed(() => {
      return useRouter().currentRoute.value.name;
    })
    return {currentRoute}
  }
}

I use this...

this.$router.history.current.path

In Composition API, this works

import { useRouter } from 'vue-router'

const router = useRouter()

let currentPathObject = router.currentRoute.value; 
 
console.log("Route Object", currentPathObject)

// Pick the values you need from the object

This is how you can access AND watch current route's name using @vue/composition-api package with Vue 2 in TypeScript.

<script lang="ts">
import { defineComponent, watch } from '@vue/composition-api';

export default defineComponent({
  name: 'MyCoolComponent',
  setup(_, { root }) {
    console.debug('current route name', root.$route.name);

    watch(() => root.$route.name, () => {
      console.debug(`MyCoolComponent- watch root.$route.name changed to ${root.$route.name}`);
    });
  },
});
</script>

I will update this answer once Vue 3.0 and Router 4.0 gets released!

I used something like this:

import { useRoute } from 'vue-router';

then declared

const route = useRoute();

Finally if you log route object - you will get all properties I used path for my goal.

In my Laravel app I created a router.js file and I can access the router object in any vue component like this.$route

I usually get the route like this.$route.path

Using composition API,

<template>
 <h1>{{Route.name}}</h1>
</template>

<script setup>
import {useRoute} from 'vue-router';

const Route = useRoute();
</script>

this.$router.currentRoute.value.name;

Works just like this.$route.name.

Using Vue 3 and Vue Router 4 with Composition API and computed:

<script setup>
    import { computed } from 'vue'
    import { useRouter } from 'vue-router'

    // computed
    const currentRoute = computed(() => {
        const router = useRouter()
        return router.currentRoute.value.name
    })
</script>

<template>
    <div>{{ currentRoute }}</div>
</template>

⚠ If you don't set a name in your router like so, no name will be displayed:

const routes = [
    { path: '/step1', name: 'Step1', component: Step1 },
    { path: '/step2', name: 'Step2', component: Step2 },
];

this is how you can get id (name) of current page in composition api (vue3):

import { useRoute } from 'vue-router';

export function useFetchPost() {
  const currentId = useRoute().params.id;
  const postTitle = ref('');

  const fetchPost = async () => {
        try {
        const response = await axios.get(
      `https://jsonplaceholder.typicode.com/posts/${currentId}`
    );
        postTitle.value = response.data.title;
      } catch (error) {
        console.log(error);
      } finally {
      }
  };

onMounted(fetchPost);

return {
  postTitle,
};

}

I'm using this method on vue 3 & vue-router 4
It works great!

<script>
import { useRoute } from 'vue-router'

export default {
    name: 'Home',
    setup() {
        const route = useRoute();
        const routeName = route.path.slice(1); //route.path will return /name

        return {
            routeName
        }
    }
};
</script>
<p>This is <span>{{ routeName }}</span></p>
Related