go-view/src/router/router-guards.ts
2021-12-16 20:08:05 +08:00

27 lines
752 B
TypeScript

import { Router } from 'vue-router';
import { PageEnum } from '@/enums/pageEnum'
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);
if (isErrorPage === -1) {
next({ name: PageEnum.ERROR_PAGE_NAME_404 })
}
next()
})
router.afterEach((to, _, failure) => {
const Loading = window['$loading'] || null;
document.title = (to?.meta?.title as string) || document.title;
Loading && Loading.finish();
})
// 错误
router.onError((error) => {
console.log(error, '路由错误');
});
}