import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router' import { store } from '../store' import layout from '../layout/index.vue' // 静态路由 export const constantRoutes: Array = [ { path: '/', component: layout, redirect: '/home', meta: { title: { '/zh-CN': '首页', '/en-US': 'Home Page' }, icon: 'ic ic-homepage-fill' }, children: [ { path: '/home', name: 'home', component: () => import(/* webpackChunkName: "home" */ '@/views/Home/home.vue'), meta: { title: { '/zh-CN': '首页', '/en-US': 'Home Page' }, icon: 'ic ic-homepage-fill' } } ] }, { path: '/login', name: '登录', component: () => import(/* webpackChunkName: "login" */ '@/views/Login/index.vue'), meta: { title: { '/zh-CN': '登录', '/en-US': 'Login' }, hidden: true, hiddenTab: true } }, { path: '/noFound', name: 'NoFound', component: () => import(/* webpackChunkName: "noFound" */ '@/views/noFound.vue'), meta: { title: { '/zh-CN': '404', '/en-US': '404' }, hidden: true, hiddenTab: true } }, { path: '/:pathMatch(.*)*', name: 'not-found', component: () => import(/* webpackChunkName: "noFound" */ '@/views/noFound.vue'), meta: { title: { '/zh-CN': '未找到', '/en-US': 'NOT FOUND' }, hidden: true, hiddenTab: true } } ] // 异步路由 export const asyncRoutes: Array = [] const router = createRouter({ history: createWebHashHistory(), // hash模式:createWebHashHistory,history模式:createWebHistory scrollBehavior: () => ({ top: 0 }), routes: constantRoutes }) router.beforeEach((to, from, next) => { const tabsOption = store.getters['tabModule/getTabsOption'] // 判断当前路由中是否已经入栈 const flag = tabsOption.findIndex((tab: { route: string }) => tab.route === to.path) > -1 if (!flag && !to.meta.hiddenTab) { store.commit('tabModule/ADD_TAB', { route: to.path, title: to.meta.title, name: to.name }) } store.commit('tabModule/SET_TAB', to.path) if (sessionStorage.getItem('auth')) { next() } else if (to.path === '/login') { console.log('/login') next() } else { console.log('unauthed into login') next({ path: '/login', query: { redirect: to.fullPath } }) } }) export default router