-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
auto-imported useRoute()
from nuxt requires use of <NuxtPage>
#14595
Comments
/cc @danielroe |
The key thing is that you should not destructure - const { params } = useRoute()
+ const route = useRoute() Once fixed, note that you were effective re-implementing a route that was always one step behind the actual route in |
I want to show Book Componenet with background (previous page), so I edited But I add Please let me know where my mistake is... app.vue: ...
- const route = useRoute();
const routeWithPopup = computed(() =>
+ historyState.value.background ? resolve(historyState.value.background) : undefined
);
... ...
+ <NuxtPage :route="routeWithPopup" />
... components/Book.vue: // When I add
// import { useRoute } from 'vue-router'
// It does work
const route = useRoute(); <!-- doesn't work with no import -->
<div>Book {{ route.params.id }}</div> |
I have the exact same issue. Could also throw in the fact that if I have Here's my code examples: Composition API <script setup>
import { useRoute, useRouter } from 'vue-router';
const route = useRoute();
const { pending, data: pages, refresh } = await useLazyFetch(`/api/sites/${route.params.siteKey}/pages`);
watch(
() => route.params.siteKey, async siteKey => {
refresh();
}
);
</script> Options API <script type="ts">
import { defineComponent } from 'vue';
export default defineComponent({
async setup() {
const route = useRoute();
const { data, refresh: refreshPages } = await useFetch(`/api/sites/${route.params.siteKey}/pages`);
return {
pages: data.value,
refreshPages
}
},
watch: {
'$route.params.siteKey': function(oldValue, newValue) {
this.refreshPages();
}
}
});
</script> In both cases, the watch sees the new value and it is in fact changed. However, the @danielroe should I open a new issue regarding the |
The issue here is that you should not import The useFetch issue you describe is already covered by #14342. |
@danielroe Regarding the Thank you for referring me to the other issue regarding useFetch! 🙏 |
|@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?
"nuxt": "3.0.0-rc.11", |
@Ischafak my workaround (it's not very pretty) is to use state management that will refresh a variable 🤔. In app.vue |
Hi there ! 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 PS: Note that the problem doesn't come, at least in my case, from destructuring. Thanks! |
demo |
@chenxch The issue you are experiencing is that the built-in |
@danielroe thx~💗 |
I just added import to composables file for workaraund: //composables/common.js //and in components we can use route as usually, they will reactive |
useRoute()
from nuxt requires use of <NuxtPage>
I'm facing the same issue, is there any fix available? |
Environment
Darwin
v18.7.0
3.0.0-rc.8
yarn@1.22.19
vite
modules
,css
,colorMode
,vite
,app
nuxt-windicss@2.5.0
,@pinia/nuxt@0.3.1
,@nuxtjs/color-mode@3.1.4
,@nuxt/image-edge@1.0.0-27657146.da85542
-
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
The text was updated successfully, but these errors were encountered: