useRouter() return undefined

See original GitHub issue

when i use useRouter break away vue hooks , it return undefined, but in the vue hooks, it return Router. so i dont know this reason, Please answer me how to use useRouter is normal js, because i want to use it in vuex image

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
cecitorrescommented, May 19, 2021

useRouter must be called inside of setup() (documentation link)

But you can use composition api for extract logic of routing

<script>
import { useRouter } from "vue-router"

const useRouterCustom = () => {
  const router = useRouter();

  const goToRoot = () => {
    router.push('/')
  }
  return {
    goToRoot
  }
}

export default {
  name: 'App',
  setup() {
    const { goToRoot } = useRouterCustom();
  }
  
}
</script>

I hope it helps, good luck!

0reactions
PeppeL-Gcommented, Sep 17, 2022

That composables should only be used in Vue components (in setup() is now documented here:

https://vuejs.org/guide/reusability/composables.html#usage-restrictions

Read more comments on GitHub >

github_iconTop Results From Across the Web

useRouter/withRouter receive undefined on query in first render
I found something: isReady: boolean - Whether the router fields are updated client-side and ready for use. Should only be used inside of ......
Read more >
Vue 3 useRouter undefined - Get Help
Hi, I want to add a simple event listen that watches for network connectivity changes in Vue 3, I have it working, but...
Read more >
Fixing Next.js router query param returning undefined on ...
If you're getting undefined query params in nextjs on the first render and want to fix ... const Test = () => {...
Read more >
[Vue.js] Parameters of router are undefined. - Medium
[Vue.js] Parameters of router are undefined. ... const route = useRoute()const router = useRouter()let companyId: Ref<string | string[]> ...
Read more >
vue3.0路由跳转useRouter执行后undefined问题-踩坑笔记
方法一注意:useRouter执行一定要放在setup方法内的顶部, ... return { ... import { useRouter } from 'vue-router'打印useRouter为undefined.
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