src/router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {
getToken } from '@/utils/auth'
import getPageTitle from '@/utils/get-page-title'
Vue.use(Router)
/* Layout */
import Layout from '@/layout'
export const constantRoutes = [ //必有路由,不用设置权限的路由
{
path: '/redirect',
component: Layout,
hidden: true,
children: [
{
path: '/redirect/:path(.*)',
component: () => import('@/views/redirect/index')
}
]
},
{
path: '/login',
component: () => import('@/views/login/index'),
hidden: true
},
{
path: '/auth-redirect',
component: () => import('@/views/login/auth-redirect'),
hidden: true
},
{
path: '/404',
component: () => import('@/views/error-page/404'),
hidden: true
},
{
path: '/401',
component: () => import('@/views/error-page/401'),
hidden: true
},
{
path: '/',
component: Layout,
redirect: '/dashboard',
children: [
{
path: 'dashboard',
component: () => import('@/views/dashboard/index'),
name: 'Dashboard',
meta: {
title: 'dashboard', icon: 'dashboard', premissiong: '/dashboard/index', affix: true }
}
]
}
]
const createRouter = () => new Router({
scrollBehavior: () => ({
y: 0 }),
routes: constantRoutes
})
const router = createRouter()
export function resetRouter () {
const newRouter = createRouter()
router.matcher = newRouter.matcher // reset router
}
// 捕获权限
router.beforeEach(async (to, from, next) => {
const hasToken = getToken()
console.log(to, 'to')
console.log('hastoken', hasToken)
NProgress.