首先找到src/main.js注释掉mock(mock是模拟数据)
// import './mock'
然后在src/laouts/BasicLayout中引入asyncRouterMap,并且改造created
import { asyncRouterMap } from '@/config/router.config.js'
//created钩子替换成以下代码
created () {
this.menus = asyncRouterMap.find((item) => item.path === '/').children
this.collapsed = !this.sidebarOpened
}
在src/router/index.js替换成以下代码
import Vue from 'vue'
import Router from 'vue-router'
import { constantRouterMap, asyncRouterMap } from '@/config/router.config'
const originalPush = Router.prototype.push
Router.prototype.push = function push (location, onResolve, onReject) {
if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
return originalPush.call(this, location).catch(err => err)
}
Vue.use(Router)
export default new Router({
mode: 'history',
base: process.env.BASE_URL,
scrollBehavior: () => ({ y: 0 }),
routes: constantRouterMap.concat(asyncRouterMap)
})
在src/permission.js中做以下修改直接放行(注释以下代码改成next())
next()
// if (store.getters.roles.length === 0) {
// store
// .dispatch('GetInfo')
// .then(res => {
// const roles = res.result && res.result.role
// store.dispatch('GenerateRoutes', { roles }).then(() => {
// // 根据roles权限生成可访问的路由表
// // 动态添加可访问路由表
// router.addRoutes(store.getters.addRouters)
// // 请求带有 redirect 重定向时,登录自动重定向到该地址
// const redirect = decodeURIComponent(from.query.redirect || to.path)
// if (to.path === redirect) {
// // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
// next({ ...to, replace: true })
// } else {
// // 跳转到目的路由
// next({ path: redirect })
// }
// })
// })
// .catch(() => {
// notification.error({
// message: '错误',
// description: '请求用户信息失败,请重试'
// })
// store.dispatch('Logout').then(() => {
// next({ path: '/user/login', query: { redirect: to.fullPath } })
// })
// })
// } else {
// next()
// }
如果请求头对Token的请求头名称有要求的话可以在src/store/mutation-type.js中进行修改或添加