fix: problem that old routes configuration not clearing when merging async routes; 🐛

修复:合并异步路由时,旧的路由配置未及时清除的问题。
master
chenghongxing 4 years ago
parent a03b37ff30
commit 9df2666304
  1. 2
      package.json
  2. 7
      src/bootstrap.js
  3. 2
      src/pages/login/Login.vue
  4. 4
      src/utils/authority-utils.js
  5. 47
      src/utils/routerUtil.js

@ -1,6 +1,6 @@
{
"name": "vue-antd-admin",
"version": "0.6.0",
"version": "0.6.1",
"homepage": "https://iczer.github.io/vue-antd-admin",
"private": true,
"scripts": {

7
src/bootstrap.js vendored

@ -1,4 +1,4 @@
import {loadRoutes, loadGuards} from '@/utils/routerUtil'
import {loadRoutes, loadGuards, setAppOptions} from '@/utils/routerUtil'
import {loadInterceptors} from '@/utils/request'
import guards from '@/router/guards'
import interceptors from '@/utils/axios-interceptors'
@ -9,12 +9,15 @@ import interceptors from '@/utils/axios-interceptors'
* @param router 应用的路由实例
* @param store 应用的 vuex.store 实例
* @param i18n 应用的 vue-i18n 实例
* @param i18n 应用的 message 实例
*/
function bootstrap({router, store, i18n, message}) {
// 设置应用配置
setAppOptions({router, store, i18n})
// 加载 axios 拦截器
loadInterceptors(interceptors, {router, store, i18n, message})
// 加载路由
loadRoutes({router, store, i18n})
loadRoutes()
// 加载路由守卫
loadGuards(guards, {router, store, i18n, message})
}

@ -120,7 +120,7 @@ export default {
//
getRoutesConfig().then(result => {
const routesConfig = result.data.data
loadRoutes({router: this.$router, store: this.$store, i18n: this.$i18n}, routesConfig)
loadRoutes(routesConfig)
this.$router.push('/dashboard/workplace')
this.$message.success(loginRes.message, 3)
})

@ -71,7 +71,9 @@ function hasAuthority(route, permissions, roles) {
function filterMenu(menuData, permissions, roles) {
menuData.forEach(menu => {
if (menu.meta && menu.meta.invisible === undefined) {
menu.meta.invisible = !hasAuthority(menu, permissions, roles)
if (!hasAuthority(menu, permissions, roles)) {
menu.meta.invisible = true
}
if (menu.children && menu.children.length > 0) {
filterMenu(menu.children, permissions, roles)
}

@ -2,6 +2,25 @@ import routerMap from '@/router/async/router.map'
import {mergeI18nFromRoutes} from '@/utils/i18n'
import Router from 'vue-router'
import deepMerge from 'deepmerge'
import basicOptions from '@/router/async/config.async'
//应用配置
let appOptions = {
router: undefined,
i18n: undefined,
store: undefined
}
/**
* 设置应用配置
* @param options
*/
function setAppOptions(options) {
const {router, store, i18n} = options
appOptions.router = router
appOptions.store = store
appOptions.i18n = i18n
}
/**
* 根据 路由配置 路由组件注册 解析路由
@ -49,12 +68,24 @@ function parseRoutes(routesConfig, routerMap) {
/**
* 加载路由
* @param router 应用路由实例
* @param store 应用的 vuex.store 实例
* @param i18n 应用的 vue-i18n 实例
* @param routesConfig 路由配置
* @param routesConfig {RouteConfig[]} 路由配置
*/
function loadRoutes({router, store, i18n}, routesConfig) {
function loadRoutes(routesConfig) {
//兼容 0.6.1 以下版本
/*************** 兼容 version < v0.6.1 *****************/
if (arguments.length > 0) {
const arg0 = arguments[0]
if (arg0.router || arg0.i18n || arg0.store) {
routesConfig = arguments[1]
console.error('the usage of signature loadRoutes({router, store, i18n}, routesConfig) is out of date, please use the new signature: loadRoutes(routesConfig).')
console.error('方法签名 loadRoutes({router, store, i18n}, routesConfig) 的用法已过时, 请使用新的方法签名 loadRoutes(routesConfig)。')
}
}
/*************** 兼容 version < v0.6.1 *****************/
// 应用配置
const {router, store, i18n} = appOptions
// 如果 routesConfig 有值,则更新到本地,否则从本地获取
if (routesConfig) {
store.commit('account/setRoutesConfig', routesConfig)
@ -66,8 +97,8 @@ function loadRoutes({router, store, i18n}, routesConfig) {
if (asyncRoutes) {
if (routesConfig && routesConfig.length > 0) {
const routes = parseRoutes(routesConfig, routerMap)
formatRoutes(routes)
const finalRoutes = mergeRoutes(router.options.routes, routes)
const finalRoutes = mergeRoutes(basicOptions.routes, routes)
formatRoutes(finalRoutes)
router.options = {...router.options, routes: finalRoutes}
router.matcher = new Router({...router.options, routes:[]}).matcher
router.addRoutes(finalRoutes)
@ -216,4 +247,4 @@ function loadGuards(guards, options) {
})
}
export {parseRoutes, loadRoutes, formatAuthority, getI18nKey, loadGuards, deepMergeRoutes, formatRoutes}
export {parseRoutes, loadRoutes, formatAuthority, getI18nKey, loadGuards, deepMergeRoutes, formatRoutes, setAppOptions}

Loading…
Cancel
Save