question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useRoute() in 'vue-router' is working but not in nuxt.

See original GitHub issue

Environment

  • Operating System: Darwin
  • Node Version: v18.7.0
  • Nuxt Version: 3.0.0-rc.8
  • Package Manager: yarn@1.22.19
  • Builder: vite
  • User Config: modules, css, colorMode, vite, app
  • Runtime Modules: nuxt-windicss@2.5.0, @pinia/nuxt@0.3.1, @nuxtjs/color-mode@3.1.4, @nuxt/image-edge@1.0.0-27657146.da85542
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/github-izskfd?file=components/Book.vue

Describe the bug

useRoute(), nuxt default provided function, has previous route information, and not updated on moving page.

But using import { useRoute } from vue-router, resolve all problems.

Is there any point I missed?

Additional context

rc.6 doesn’t have problems but rc.8 has.

It just occurred as soon as updating from rc.6 to rc.8

Logs

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:7
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ValJedcommented, Oct 31, 2022

Hi there ! Tiny ping because I’m also experiencing this issue. Nuxt version 3.0.0-rc.11

Code example:

// experiences/index.js

<template>
  <div>
    <h1>Experiences</h1>
    <ul>
      <li>
        <NuxtLink to="experiences/experience1">
          <div>
            <img src="/images/exp1.png" />
          </div>
        </NuxtLink>
      </li>
      <li>
        <NuxtLink to="experiences/experience2">
          <img src="/images/exp2.png"  />
        </NuxtLink>
      </li>
    </ul>
  </div>
</template>
// experiences/[experience].js
<script setup>
import experiences from '@/data/experiences'
import { useRoute } from 'vue-router' // With this line it's working as expected

const route = useRoute()

const current = route.params.experience // This param is loaded once but never updated while the route changes
const experience = experiences[current] // So this variable always stays the same
</script>

For me too, the only way of making it work is to import manually import { useRoute } from 'vue-router'. When counting on the auto import it simply doesn’t work.

PS: Note that the problem doesn’t come, at least in my case, from destructuring.

Thanks!

2reactions
Ischafakcommented, Oct 25, 2022

|@danielroe hi, in my application i am changing page with NuxtLink and i am using useRoute in footer. when first load the page useRoute data works very well but after i change page fullpath doesnt change. How can i fix this can anyone help me please?

<script setup>
const route = useRoute()

const isPricing = computed(() => {
  return route.fullPath === "/blablabla"
})
</script>

“nuxt”: “3.0.0-rc.11”,

Read more comments on GitHub >

github_iconTop Results From Across the Web

Route query is undefined when using useRoute() in setup() for ...
5 and I am experiencing an issue where useRoute() appears to retieve the route correctly but the query of the route is undefined,...
Read more >
useRoute - Nuxt Composition API
In Vue 3, vue-router exports composition functions for accessing the current route and router. These helpers provide an equivalent whilst using Nuxt 2....
Read more >
Vue Router and the Composition API
import { useRouter, useRoute } from 'vue-router' export default { setup() { const router = useRouter() const route = useRoute() function ...
Read more >
Named Routes - Vue Router
Watch a free video lesson on Vue School. Alongside the path , you can provide a name to any route. This has the...
Read more >
Dynamic Routing | Vue Router
route or route = useRoute() (inside a setup) router.replace(router. ... you should not call router.replace() but trigger a redirection by returning the new ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found