import Vue from 'vue' import VueRouter from 'vue-router' // 路由3+版本的异常处理 const originalPush = VueRouter.prototype.push VueRouter.prototype.push = function push(location) { return originalPush.call(this, location).catch(err => err) } const originalReplace = VueRouter.prototype.replace; VueRouter.prototype.replace = function replace(location) { return originalReplace.call(this, location).catch(err => err); } // 公共页面 import main from '@views/main' import error404 from '@views/404' // 子路由 import yRouter from '@router/ylf'; import lRouter from '@router/lzx'; import zRouter from '@router/zm'; Vue.use(VueRouter) const routes = [ { path: '/', meta: { requireAuth: true, // 添加该字段,表示进入这个路由是需要登录的 }, component: main, children:[ { // 首页 path:'/', component: r => require.ensure([], () => r(require('@views/index/index')), 'indexM') }, ...yRouter, ...lRouter, ...zRouter ] }, { path: '/404', // 页面不存在的情况下会跳到404页面 meta: { requireAuth: true, // 添加该字段,表示进入这个路由是需要登录的 }, name: 'error404', component: error404, } ] const router = new VueRouter({ mode: 'history', base: process.env.BASE_URL, routes }) /** * 路由的钩子函数,处理是否登录的判断 * **/ router.beforeEach((to, from, next) => { // 路由地址不存在的处理办法 if (to.matched.length === 0) { // 如果未匹配到路由 // sessionStorage.removeItem("btrh_sxsd_locationHref"); next('/404') // 如果上级也未匹配到路由则跳转登录页面,如果上级能匹配到则转上级路由 } if (to.matched.some(r => r.meta.requireAuth)) { let userinfo = JSON.parse(sessionStorage.getItem("btrh_sxsd_userinfo")); userinfo = ""; if(userinfo !== null){ // if(to.path !== "/"){ // // 判断当前菜单是否有权限打开,如果无权限,自动退出 // routerCheck(userinfo.roleId, to.path, next); // }else{ // next(); // } next(); return; } next("/login"); } else { next(); } }) export default router