Skip to content
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

fix: watch and useRoute() not working #787

Open
laurentfirdion opened this issue Mar 5, 2024 · 1 comment
Open

fix: watch and useRoute() not working #787

laurentfirdion opened this issue Mar 5, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@laurentfirdion
Copy link

laurentfirdion commented Mar 5, 2024

๐Ÿ› The bug
When using useRoute() and a watcher it throws a "This must be called within a setup function" error when browsing inside the app

๐Ÿ› ๏ธ To reproduce
Declare route and add a watcher inside a setup function

export default defineComponent({
setup() {
const route = useRoute()
watch(route, (to) => {
// do something with the watch
})
}
})

First load is ok but it crashes when changing page inside the nuxt app

๐ŸŒˆ Expected behaviour
I don't know if there is a workaround for this situation or a bug to be fixed. My understanding is that vue getCurrentInstance() has many limitation and poor documentation except for that one doc that warns that it shouldn't be used at all (only found on that ru version of vue documentation).

@laurentfirdion laurentfirdion added the bug Something isn't working label Mar 5, 2024
@laurentfirdion
Copy link
Author

My current workaround is to not use useRoute() (or useRouter for the same reason) and rather register getCurrentInstance() inside my component

setup() {
const vm = getCurrentInstance()!.proxy
watch(() => vm.$route.name!, (to) => { // watch callback })
}

It works flawlessly.

N.B. I give up on using wrapProperty also after encountered errors / strange behaviour

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants