Skip to content

Example of using useQuery inside the vue-router beforeEach callback. #6987

Answered by andredewaard
andredewaard asked this question in Q&A
Discussion options

You must be logged in to vote

i think i got it, Shouldn't use the useQuery stuff since that is only for components (i guess) but fetchQuery should work:

router.beforeEach(async (route) => {
  const queryClient = inject('queryClient', new QueryClient())
  const data = await queryClient.fetchQuery({
    queryKey: ['auth', 'getMe'],
    queryFn: async () => {
      const data = await authService().getMe()
      return AuthUserModel(data.data)
    },
  })
  if (route.meta.requiresAuth && !data.id) {
    return { name: 'auth-login' }
  }
})

with these options so it doesnt need to refetch everytime someone navigates to a different page

const DEFAULT_STALE_TIME = 1000 * 60 * 60 // 1 hour
const DEFAULT_GC_TIME = DEFAULT_STALE…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by andredewaard
Comment options

You must be logged in to vote
1 reply
@hope-ag
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants