Skip to content

Instantly share code, notes, and snippets.

@TanAlex
Created June 7, 2019 00:35
Show Gist options
  • Save TanAlex/e9d103b4688d79d8ff9ba59d1e489347 to your computer and use it in GitHub Desktop.
Save TanAlex/e9d103b4688d79d8ff9ba59d1e489347 to your computer and use it in GitHub Desktop.
[vue router snippet]vue router related #vue #vue_router
/*
* to.matched is array for all the route it matches, use m.meta to get flags
* remember to use next() at the end !
* next({name: 'new-route'}) to redirect
*/
router.beforeEach(async (to, from, next) => {
if (to.matched.some(m => m.meta.needProject)) {
const result = await apolloClient.query({
query: PROJECT_CURRENT,
fetchPolicy: 'network-only'
})
if (!result.data.projectCurrent) {
next({ name: 'project-select' })
return
} else {
await apolloClient.mutate({
mutation: CURRENT_PROJECT_ID_SET,
variables: {
projectId: result.data.projectCurrent.id
}
})
}
}
next()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment