import Vue from 'vue';
import { Component } from 'vue-property-decorator';
import VueRouter, { RawLocation, Route, RouteConfig } from 'vue-router';
Component.registerHooks(['beforeRouteEnter', 'beforeRouteLeave', 'beforeRouteUpdate']);
const home = () => import('@/views/home/Home.vue');
// 解决路由重复跳转报错
VueRouter.prototype.push = async function(location: RawLocation) {
/* eslint-disable no-extra-parens */
return ((originalPush.call(this, location) as unknown) as Promise<Route>).catch(err => err);
};
Vue.use(VueRouter);
const routes: Array<RouteConfig> = [
{
path: '/',
redirect: '/home',
},
{
path: '/home',
component: home,
},
//其他
{
path: '*',
redirect: '/',
},
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes,
});
// 路由加载错误
router.onError(err => {
console.log('VueRouter.onError', err);
});
router.beforeEach((to, from, next) => {
ajax.cancelAllRequest(); // 删除所有请求
});
export default router;
网友评论