import Vue from "vue"; import VueRouter from "vue-router"; import Layouts from "../layouts"; Vue.use(VueRouter); const originalPush = VueRouter.prototype.push VueRouter.prototype.push = function push(location) { return originalPush.call(this, location).catch(err => err) } /** * path: "/login", ------页面地址 component: () => import("../views/login"), ------组件地址 meta: { title: "登录", ------页面标题 icon: "el-icon-user-solid", ------菜单图标 oneself: true, ------是否在单独页面打开 hide: true, ------是否隐藏改菜单 } */ const routes = [ { path: "", redirect: "login", component: Layouts, children: [ { path: "/home", meta: { title: "首页", oneself: true, hide: true, privilege: 'home' }, component: () => import("../view/Home"), }, { path: "/login", meta: { title: "登录", oneself: true, hide: true, privilege: 'login' }, component: () => import("../view/login"), }, ] } ]; const router = new VueRouter({ mode: "hash", base: process.env.BASE_URL, routes, }); // 前置路由拦截器 router.beforeEach((to, from, next) => { console.log(to.path); // 没有登录并且要去的页面不是登录页面,强制跳转到登录 if (!localStorage.getItem('extra') && to.path !== '/login') { next('/login'); } else { if (to.path === '/login') { localStorage.clear(); } next(); } }); export default router;