How can I access current route name, reactively, with Vue Router using Vue Composition API in Vue 3 with TypeScript?
We can use this. $router. currentRoute. path property in a vue router component to get the current path.
It comes from the vue-router file such as path /something/:slug . Double check my code where I put console. log('route', route. params) .
With Vue Router, you can use its router. push() function to programmatically navigate between routes on your site. You can either call push() with a string path, or with an object containing either the path or name of the route.
Here are examples using Vue 3.0 and Vue Router v4.0.0-beta.12 with Composition API syntax:
<script lang="ts">
import { defineComponent, computed, watch } from 'vue';
import { useRoute } from 'vue-router';
export default defineComponent({
name: 'MyCoolComponent',
setup() {
const route = useRoute();
console.debug(`current route name on component setup init: ${route.name}`);
// You could use computed property which re-evaluates on route name updates
// const routeName = computed(() => route.name);
// You can watch the property for triggering some other action on change
watch(() => route.name, () => {
console.debug(`MyCoolComponent - watch route.name changed to ${route.name}`);
// Do something here...
// Optionally you can set immediate: true config for the watcher to run on init
//}, { immediate: true });
});
return { route };
},
});
</script>
<template>
<p>Current route name: {{ route.name }}</p>
</template>
Or by using the currently experimental Script Setup syntax, SFC Composition API Syntax Sugar, for Composition API:
<script setup lang="ts">
import { computed, watch } from 'vue';
import { useRoute } from 'vue-router';
export const name = 'MyCoolComponent';
export const route = useRoute();
console.debug(`current route name on component setup init: ${route.name}`);
// You could use computed property which re-evaluates on route name updates
//export const routeName = computed(() => route.name);
// You can watch the property for triggering some other action on change
watch(() => route.name, () => {
console.debug(`MyCoolComponent - watch route.name changed to ${route.name}`);
// Do something here...
// Optionally you can set immediate: true config for the watcher to run on init
//}, { immediate: true });
});
</script>
<template>
<p>Current route name: {{ route.name }}</p>
</template>
Here's my example which is for watching route params instead. This question comes up when generally searching for how to watch route params in Vue 3.
<script setup lang="ts">
import axios from 'axios'
import { ref, onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const page = ref<any>({})
const fetchPage = async () => {
console.log('route', route.params)
const { data } = await axios.get(
`/api/${route.params.locale}/pages/${route.params.slug}`,
{
params: {
include: 'sections,documents,courses',
},
}
)
page.value = data.data
}
onMounted(() => {
fetchPage()
})
watch(() => route.params.slug, fetchPage)
</script>
In my example, route.name
doesn't change but route.params.slug
changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With