Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access current route name reactively in Vue Composition API in TypeScript?

How can I access current route name, reactively, with Vue Router using Vue Composition API in Vue 3 with TypeScript?

like image 485
ux.engineer Avatar asked Sep 08 '20 20:09

ux.engineer


People also ask

How do I find out my current route Vue?

We can use this. $router. currentRoute. path property in a vue router component to get the current path.

How do I get the route name in Vue 3?

It comes from the vue-router file such as path /something/:slug . Double check my code where I put console. log('route', route. params) .

How do I push my Vue Router?

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.


2 Answers

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>
like image 107
ux.engineer Avatar answered Nov 14 '22 23:11

ux.engineer


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.

like image 36
agm1984 Avatar answered Nov 15 '22 01:11

agm1984