vue-router#NavigationGuard TypeScript Examples
The following examples show how to use
vue-router#NavigationGuard.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: authGuard.ts From auth0-ts-vue-example with MIT License | 6 votes |
authGuard: NavigationGuard = (to, from, next) => {
const authService = getInstance()
const fn = () => {
// Unwatch loading
unwatch && unwatch()
// If the user is authenticated, continue with the route
if (authService.isAuthenticated) {
return next()
}
// Otherwise, log in
authService.loginWithRedirect({ appState: { targetUrl: to.fullPath } })
}
// If loading has already finished, check our auth state using `fn()`
if (!authService.loading) {
return fn()
}
// Watch for the loading property to change before we check isAuthenticated
const unwatch = authService.$watch('loading', (loading: boolean) => {
if (loading === false) {
return fn()
}
})
}
Example #2
Source File: index.ts From vue-storefront-1 with MIT License | 5 votes |
private static _extendRouter (routerInstance, routes?: RouteConfig[], beforeEach?: NavigationGuard, afterEach?: NavigationGuard): void {
if (routes) {
setupMultistoreRoutes(config, routerInstance, routes)
}
if (beforeEach) routerInstance.beforeEach(beforeEach)
if (afterEach) routerInstance.afterEach(afterEach)
}