本文整理汇总了TypeScript中vue-router.beforeEach函数的典型用法代码示例。如果您正苦于以下问题:TypeScript beforeEach函数的具体用法?TypeScript beforeEach怎么用?TypeScript beforeEach使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了beforeEach函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: getAllRoutes
export const createRouter = (): VueRouter => {
const routes: IRouteConfigAugmented[] = getAllRoutes();
const router = new VueRouter({
mode: 'hash',
routes,
scrollBehavior(to, from, savedPosition) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({x: 0, y: 0});
}, 400); // scroll to top for each new route
});
},
});
router.beforeEach((to, from, next) => {
const route: IRouteConfigAugmented = _.find(routes, (rt) => rt.path === to.fullPath);
if (route) {
StoreW.setTitle(route.text);
StoreW.setRoute(route);
}
next();
});
return router;
};
示例2: VueRouter
const createRouter = () => {
const router = new VueRouter(ROUTER_SETTING);
// manually hook: page not scroll to top when router changes
// github issue: https://github.com/vuejs/vue-router/issues/173
router.beforeEach((route, redirect, next) => {
window.scrollTo(0, 0);
next();
});
router.afterEach((route: Route) => {
console.info(`${new Date()}: ${route.path}`);
});
return router;
};
示例3: VueRouter
var router = new VueRouter();
router.map({
'/login': {
name: 'login',
component: loginState,
auth: false
},
'/dashboard': {
name: 'dashboard',
component: dashState,
auth: true
},
'/transactions': {
name: 'transactions',
component: transState,
auth: true
}
});
router.beforeEach((trans:any) => {
if (trans.to.auth && !Auth.isLogged) {
trans.redirect('/login');
} else {
trans.next();
}
});
export default router;
示例4: mounted
{ path: '/implicit/callback', component: Auth.handleCallback() },
{
path: '/protected',
component: {
name: 'protected',
template: '<div>Protected Route</div>'
},
meta: {
requiresAuth: true
}
},
]
});
const redirectGuard = Vue.prototype.$auth.authRedirectGuard();
router.beforeEach(redirectGuard);
const component = Vue.extend({
mounted() {
const authd = this.$auth.isAuthenticated();
const userInfo = this.$auth.getUser();
const handleAuth = this.$auth.handleAuthentication();
const path = this.$auth.getFromUri();
const idToken = this.$auth.getIdToken();
const accessToken = this.$auth.getAccessToken();
},
methods: {
logout() {
const logoutPromise = this.$auth.logout();
},
redirect() {
示例5: if
})
router.beforeEach((to, from, next) => {
if (to.matched.some((record) => record.meta.authorizedZone)) {
if (!storage.getItem(config.tokenName)) {
// console.log('Checkpoint #1')
next({
path: '/login',
// query: { redirect: to.fullPath }
})
} else {
// console.log('Checkpoint #2')
next()
}
} else if (to.matched.some((record) => record.meta.loginPage)) {
if (storage.getItem(config.tokenName)) {
// console.log('Checkpoint #3')
next({
path: '/',
// query: { redirect: to.fullPath }
})
} else {
// console.log('Checkpoint #4')
next()
}
} else {
// console.log('Checkpoint #5')
next() // make sure to always call next()!
}
})
export default router