diff --git a/src/views/nested/menu1/index.vue b/src/components/ParentView/index.vue
similarity index 93%
rename from src/views/nested/menu1/index.vue
rename to src/components/ParentView/index.vue
index 98240ae..d91599f 100644
--- a/src/views/nested/menu1/index.vue
+++ b/src/components/ParentView/index.vue
@@ -1,3 +1,3 @@
-
-
-
+
+
+
diff --git a/src/layout/components/Sidebar/index.vue b/src/layout/components/Sidebar/index.vue
index c029f61..50a35e9 100644
--- a/src/layout/components/Sidebar/index.vue
+++ b/src/layout/components/Sidebar/index.vue
@@ -12,7 +12,7 @@
:collapse-transition="false"
mode="vertical"
>
-
+
@@ -28,7 +28,7 @@ export default {
components: { SidebarItem, Logo },
computed: {
...mapGetters([
- 'permission_routers',
+ 'sidebarRouters',
'sidebar'
]),
activeMenu() {
diff --git a/src/router/index.js b/src/router/index.js
index 7904706..7410141 100644
--- a/src/router/index.js
+++ b/src/router/index.js
@@ -53,12 +53,17 @@ router.beforeEach((to, from, next) => {
export const loadMenus = (next, to) => {
buildMenus().then(res => {
- const asyncRouter = filterAsyncRouter(res)
- asyncRouter.push({ path: '*', redirect: '/404', hidden: true })
- store.dispatch('GenerateRoutes', asyncRouter).then(() => { // 存储路由
- router.addRoutes(asyncRouter) // 动态添加可访问路由表
+ const sdata = JSON.parse(JSON.stringify(res))
+ const rdata = JSON.parse(JSON.stringify(res))
+ const sidebarRoutes = filterAsyncRouter(sdata)
+ const rewriteRoutes = filterAsyncRouter(rdata, true)
+ rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
+
+ store.dispatch('GenerateRoutes', rewriteRoutes).then(() => { // 存储路由
+ router.addRoutes(rewriteRoutes) // 动态添加可访问路由表
next({ ...to, replace: true })
})
+ store.dispatch('SetSidebarRouters', sidebarRoutes)
})
}
diff --git a/src/store/getters.js b/src/store/getters.js
index 376e2a8..0394a36 100644
--- a/src/store/getters.js
+++ b/src/store/getters.js
@@ -19,6 +19,7 @@ const getters = {
updateAvatarApi: state => state.api.updateAvatarApi,
qiNiuUploadApi: state => state.api.qiNiuUploadApi,
sqlApi: state => state.api.sqlApi,
- swaggerApi: state => state.api.swaggerApi
+ swaggerApi: state => state.api.swaggerApi,
+ sidebarRouters: state => state.permission.sidebarRouters
}
export default getters
diff --git a/src/store/modules/permission.js b/src/store/modules/permission.js
index e73e290..1f62c99 100644
--- a/src/store/modules/permission.js
+++ b/src/store/modules/permission.js
@@ -1,41 +1,76 @@
import { constantRouterMap } from '@/router/routers'
import Layout from '@/layout/index'
+import ParentView from '@/components/ParentView'
const permission = {
state: {
routers: constantRouterMap,
- addRouters: []
+ addRouters: [],
+ sidebarRouters: []
},
mutations: {
SET_ROUTERS: (state, routers) => {
state.addRouters = routers
state.routers = constantRouterMap.concat(routers)
+ },
+ SET_SIDEBAR_ROUTERS: (state, routers) => {
+ state.sidebarRouters = routers
}
},
actions: {
GenerateRoutes({ commit }, asyncRouter) {
commit('SET_ROUTERS', asyncRouter)
+ },
+ SetSidebarRouters({ commit }, sidebarRouter) {
+ commit('SET_SIDEBAR_ROUTERS', sidebarRouter)
}
}
}
-export const filterAsyncRouter = (routers) => { // 遍历后台传来的路由字符串,转换为组件对象
+export const filterAsyncRouter = (routers, isRewrite = false) => { // 遍历后台传来的路由字符串,转换为组件对象
return routers.filter(router => {
+ if (isRewrite && router.children) {
+ router.children = filterChildren(router.children)
+ }
if (router.component) {
if (router.component === 'Layout') { // Layout组件特殊处理
router.component = Layout
+ } else if (router.component === 'ParentView') {
+ router.component = ParentView
} else {
const component = router.component
router.component = loadView(component)
}
}
if (router.children && router.children.length) {
- router.children = filterAsyncRouter(router.children)
+ router.children = filterAsyncRouter(router.children, router, isRewrite)
}
return true
})
}
+function filterChildren(childrenMap) {
+ var children = []
+ childrenMap.forEach((el, index) => {
+ if (el.children && el.children.length) {
+ if (el.component === 'ParentView') {
+ el.children.forEach(c => {
+ c.path = el.path + '/' + c.path
+ if (c.children && c.children.length) {
+ children = children.concat(filterChildren(c.children, c))
+ return
+ }
+ children.push(c)
+ })
+ childrenMap.splice(index, 1)
+ return
+ }
+ }
+ children = children.concat(el)
+ })
+ return children
+}
+
export const loadView = (view) => {
return (resolve) => require([`@/views/${view}`], resolve)
}
diff --git a/src/views/nested/menu1/menu1-1/index.vue b/src/views/nested/menu1/menu1-1/index.vue
index 8c323ec..20ff93b 100644
--- a/src/views/nested/menu1/menu1-1/index.vue
+++ b/src/views/nested/menu1/menu1-1/index.vue
@@ -1,5 +1,15 @@
-
+
+