How to get current route name in Nuxt 2 and 3?
Asked Answered
E

8

70

I'm using Nuxt.js for building a static website.

How to access in component's script code currently displayed route name (I would like to avoid reading direct url from browser location)?

Can I somehow access $route.name ?

Evanesce answered 25/6, 2017 at 16:57 Comment(1)
Yes you should be able to access it in component in way like this.$route.nameAugust
S
134

In Nuxt2

yes you can use vuejs route objects like $route.name or $route.path

$nuxt.$route.path

return current path

$nuxt.$route.name

The name of the current route, if it has one.

Route Object Properties

A route object represents the state of the current active route. It contains parsed information of the current URL and the route records matched by the URL.

  • $route.path

  • type: string

  • A string that equals the path of the current route, always resolved as an absolute path. e.g. "/foo/bar".

  • $route.fullPath

  • type: string

  • The full resolved URL including query and hash.

**

And if you want to get the url params. Like this : enter image description here You do this:

  data() {
    return {
       zone: this.$nuxt.$route.query.zone,
       jour: this.$nuxt.$route.query.jour
        
    }   },

**

Stent answered 25/6, 2017 at 19:53 Comment(1)
My URL contains some characters after a hash sign. Not path nor name returns the value after # /my/url/#somemore only /my/url/ is returnedOlympium
S
24

In Vue2

An alternative way is to use either of the following:

  • this.$route.path → Example on http://localhost:3000 , {{this.$route.path}} will print /
  • this.$route.name → Example on http://localhost:3000, {{this.$route.name}} will print index
Spectacular answered 17/12, 2018 at 8:52 Comment(0)
T
20

With Nuxt3 and Composition API, you can achieve that with this

<script setup>
const route = useRoute()
console.log('current name', route.name)
</script>

Or with Options API

<script>
export default {
  mounted () {
    console.log('current name', this.$route.name)
  },
}
</script>

As shown in my previous answer here: https://mcmap.net/q/236157/-how-to-get-route-url-params-in-a-page-in-nuxt2-and-3

Tetravalent answered 12/5, 2022 at 8:45 Comment(2)
For me, this doesnt work. I'm trying it in the stup, and it always says it's undefinedVictim
@Victim feel free to open a new question with some relevant code.Tetravalent
A
6

for Nuxt 3

export default ({
   setup () {
    const route = useRoute()
    return {
      route
    }
  }
})

after in template

{{ route.name }}
Astringent answered 28/11, 2022 at 14:58 Comment(0)
C
5

For Nuxt 3 you can use currentRoute from router.

const { currentRoute } = useRouter();
const routeName = currentRoute.value.name;
Coriolanus answered 28/2, 2023 at 13:17 Comment(0)
F
2

for Nuxt v2 useRouter composition API

import { computed, defineComponent, useRoute } from '@nuxtjs/composition-api'

export default defineComponent({
   setup() {
       const route = useRoute()
       const routeName = computed(() => route.value.name)
       return { routeName }
   },
})
Fireweed answered 6/8, 2021 at 12:34 Comment(0)
K
0

Nuxt 3: Vue 3 and setup pattern version

<script setup>
    const route = useRoute(); // built-in composable
    // route.name

    useHead( function() {
        return {
            htmlAttrs: {
                "data-route": route.name,
            },
        };
    });
</script>

Here's a way you can inject it. For dynamic routes, things might be a little more tricky

Kellogg answered 5/11, 2023 at 17:7 Comment(0)
S
0

Just tried {{ $route.name }} in Nuxt3 and got index. No need for any js code.

Saccharo answered 9/1 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.