import { createRouter, createWebHistory, createWebHashHistory, } from 'vue-router'; import { ElMessage } from 'element-plus'; import NProgress from 'nprogress'; //页面顶部加载进度条 import 'nprogress/nprogress.css'; //此处注释store import { Store } from '@/store/index'; NProgress.configure({ showSpinner: false }); var store = new Store() // 公共路由 export const constantRoutes = [ /* *** 添加router路由 *** 11.23日修改 */ { path: '/home', component: () => import('/src/views/Main/MainView.vue'), children: [{ path: 'Map3d', name: 'Map3d', component: () => import('../views/Map3d/Map3DMain.vue'), meta: { keepAlive: true // 需要缓存 } }, { path: 'table', name: 'table', component: () => import('../views/Table/Table.vue'), meta: { keepAlive: false // 不需要缓存 } }, { path: 'echarts', name: 'echarts', component: () => import('../views/Charts/Charts.vue'), meta: { keepAlive: false // 不需要缓存 } }, { path: 'SysAdmin', name: 'SysAdmin', component: () => import('../views/SysAdmin/SysAdmin.vue'), meta: { keepAlive: false // 不需要缓存 } }, { path: '', name: 'map', component: () => import('../views/Map3d/Map3DMain.vue'), meta: { keepAlive: true // 需要缓存 } } ] }, { path: '/login', component: () => import('/src/views/Login/Login.vue') }, { path: '/', component: () => import('/src/views/Login/Login.vue') } ]; // 错误路由 const errorRoute = { path: '/:pathMatch(.*)', redirect: '/404' } const router = createRouter({ history: createWebHashHistory(), routes: constantRoutes }) // 白名单列表 const whiteList = ['/login'] // 路由加载前(全局路由前置守卫) router.beforeEach((to, from, next) => { console.log('store', store.token) NProgress.start(); // token存在的情况 if (store.token || to.path=="/login") { next(); NProgress.done(); } else { // 没有token next('/login') // 否则全部重定向到登录页 NProgress.done(); } }); // 路由加载后(全局路由后置守卫) router.afterEach(() => { NProgress.done(); }); // 获取扁平化路由,将多级路由转换成一级路由 // export const getKeepAliveRoutes = (rs, breadcrumb) => { // const routerList = [] // rs.forEach((item) => { // if (item.meta.title) { // // breadcrumb.push(item.meta.title) // } // if (item.children && item.children.length > 0) { // routerList.push(...getKeepAliveRoutes(item.children, breadcrumb)) // } else { // // item.meta.breadcrumb.push(...breadcrumb) // routerList.push(item) // } // // breadcrumb.pop() // }) // return routerList // } // 加载vue组件 // const layoutModules = // import.meta.glob('/src/views/**/*.vue') // 根据路径,动态获取vue组件 // const getDynamicComponent = (path) => { // const component = layoutModules[`/src/views/${path}.vue`] // if (!component) { // console.error('组件不存在,路径为:', path) // } // return component // } // 根据菜单列表,生成路由数据 // export const generateRoutes = (menuList) => { // const routerList = [] // menuList.forEach((menu) => { // let component; // let path; // if (menu.children && menu.children.length > 0) { // component = () => import('@/layouts/backend/index.vue') // path = '/p/' + menu.menuId // } else { // component = getDynamicComponent(menu.component) // path = menu.menuUrl // } // const route = { // path: path, // name: menu.menuName, // component: component, // children: [], // meta: { // title: menu.menuName, // icon: menu.menuIcon, // id: '' + menu.menuId, // cache: true, // _blank: menu.openStyle === 1, // breadcrumb: [] // } // } // // 有子菜单的情况 // if (menu.children && menu.children.length > 0) { // route.children?.push(...generateRoutes(menu.children)) // } // routerList.push(route) // }) // return routerList // } export default router;