Skip to content
Discussion options

You must be logged in to vote

Here's my answer from StackOverflow to the same question: https://stackoverflow.com/a/71730482/9260314

Ideally, you shouldn't need the router outside Vue components, outside boot files, outside src/router/index.js, outside src/store/index.js, and except the places where you define a function that gets called in Vue files(e.g. composables/helpers). Here are all the ways to access the router, both inside and outside SFCs:

Inside .vue files:

  • Composition API(inside setup() or <script setup>): const router = useRouter()
  • Options API: this.$router
  • Inside the <template> section: @whatever="$router.push('...')"

Inside boot files:

// src/boot/whatever.js
export default boot(({ router }) => {
  r…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@hzlzsm
Comment options

Answer selected by hzlzsm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants