Created
June 7, 2019 00:35
-
-
Save TanAlex/e9d103b4688d79d8ff9ba59d1e489347 to your computer and use it in GitHub Desktop.
[vue router snippet]vue router related #vue #vue_router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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