goview_vue/src/router/router-guards.ts

27 lines
752 B
TypeScript
Raw Normal View History

2021-12-10 14:11:49 +08:00
import { Router } from 'vue-router';
import { PageEnum } from '@/enums/pageEnum'
2021-12-10 14:11:49 +08:00
2021-12-16 20:08:05 +08:00
2021-12-10 14:11:49 +08:00
export function createRouterGuards(router: Router) {
// 前置
router.beforeEach(async (to, from, next) => {
const Loading = window['$loading'] || null;
Loading && Loading.start();
const isErrorPage = router.getRoutes().findIndex((item) => item.name === to.name);
2021-12-10 14:11:49 +08:00
if (isErrorPage === -1) {
next({ name: PageEnum.ERROR_PAGE_NAME_404 })
2021-12-10 14:11:49 +08:00
}
next()
})
router.afterEach((to, _, failure) => {
const Loading = window['$loading'] || null;
2021-12-14 20:00:20 +08:00
document.title = (to?.meta?.title as string) || document.title;
2021-12-10 14:11:49 +08:00
Loading && Loading.finish();
})
// 错误
router.onError((error) => {
console.log(error, '路由错误');
});
}