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:
https://i.stack.imgur.com/7hNhn.png
Image to show what i want
this.$route.name
.
beforeEach
hook in the routes file, see this: stackoverflow.com/questions/49685780/…
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}
}
}
setup()
, I just used computed: { route: () => useRoute() }
and then {{ route.name }}
in the template
I use this...
this.$router.history.current.path
$router
is .currentRoute.value.path
as described above
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 use this...
this.$route.name
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
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 },
];
Success story sharing
$router.currentRoute
(with an additionalr
at the end). This command returns the$route
object. See also router.vuejs.org/api/#router-currentroute and router.vuejs.org/api/#the-route-objectthis.$route.path
. Try andconsole.log(this.$route)
and see if there is anything else you would prefer.