Skip to content
This repository was archived by the owner on May 12, 2020. It is now read-only.

Commit 9cebe36

Browse files
committed
feat(route): implement dynamic routes rendering based on server response
1 parent f372922 commit 9cebe36

9 files changed

Lines changed: 92 additions & 3 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template>
2+
<div class="admin__dashboard">
3+
{{info}}
4+
<router-link to="table">table</router-link>
5+
</div>
6+
</template>
7+
8+
<script>
9+
export default {
10+
data () {
11+
return {
12+
info: 'this is /pages/admin/dashboard'
13+
}
14+
}
15+
}
16+
</script>
17+
<style lang='scss' scoped>
18+
</style>

src/pages/Admin/Table/index.vue

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template>
2+
<div class="admin__table">
3+
{{info}}
4+
<router-link to="/pages/common/user">user center</router-link>
5+
</div>
6+
</template>
7+
8+
<script>
9+
export default {
10+
data () {
11+
return {
12+
info: 'this is /pages/admin/table'
13+
}
14+
}
15+
}
16+
</script>
17+
<style lang='scss' scoped>
18+
</style>

src/pages/Common/User.vue

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template>
2+
<div class="common__user">
3+
{{info}}
4+
<router-link to="/home">home</router-link>
5+
</div>
6+
</template>
7+
8+
<script>
9+
export default {
10+
data () {
11+
return {
12+
info: 'this is /pages/common/user'
13+
}
14+
}
15+
}
16+
</script>
17+
<style lang='scss' scoped>
18+
</style>

src/pages/Home.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<template>
22
<div class="home">
33
This is home page.
4+
<router-link to="/pages/admin/dashboard">admin dashboard</router-link>
45
</div>
56
</template>
67

src/permission/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ router.beforeEach((to, from, next) => {
3535
type: 'error'
3636
})
3737
.then(() => next({
38-
path: `/login?redirect=${from.path}`,
38+
path: `/login?redirect=${to.path}`,
3939
replace: true
4040
}))
4141
NProgress.done()

src/router/components/importer.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default function createImporters (components) {
99
if (!importers[chunkName]) {
1010
importers[chunkName] = () => import(
1111
/* webpackChunkName: 'async/[request][index]' */
12-
`SOURCE/${path}`
12+
`SOURCE/${createCamelPath(path.replace(/^\//, ''), 1)}`
1313
)
1414
}
1515
})
@@ -30,3 +30,23 @@ export function createChunkName (path) {
3030
})
3131
return normalizePathSection.join('')
3232
}
33+
34+
/**
35+
* @description convert path to camelCase path, eg. /aa/bb ---> /Aa/Bb
36+
* @param {String} path original path
37+
* @param {Number} startIndex reduce should start convert when startIndex equal
38+
* to current index, including prefix slash character.
39+
* eg. createCamelPath('aaa/bbb/ccc', 1) ==> "/aaa/Bbb/Ccc"
40+
* eg. createCamelPath('/aaa/bbb/ccc', 2) ==> "/aaa/Bbb/Ccc"
41+
*/
42+
export function createCamelPath (path, startIndex) {
43+
return path.split('/')
44+
.reduce((result, cur, currentIndex) => {
45+
if (!cur) return result.concat('')
46+
if (cur && currentIndex >= startIndex) {
47+
return result.concat(cur.replace(/^[a-z]/, key => key.toUpperCase()))
48+
}
49+
return result.concat(cur)
50+
}, [])
51+
.join('/')
52+
}

src/store/modules/login/actions.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { userLogin, fetchDynamicRoutes } from 'API'
22
import types from './mutations/types'
3+
import router from 'ROUTER'
34

45
export default {
56
userLogin ({ commit }, { username, password, vm }) {
@@ -34,8 +35,9 @@ export default {
3435
})
3536
.then(({ routes }) => routes)
3637
},
37-
createGlobalRoutes ({ commit }, routes) {
38+
createGlobalRoutes ({ commit, getters }, routes) {
3839
commit(types.SET_DYNAMIC_ROUTES, routes)
3940
commit(types.SET_ALL_ROUTES)
41+
router.addRoutes(getters.dynamicRoutes)
4042
}
4143
}

src/store/modules/login/getters.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default {
2+
// used to add user private routes to global routes map.
3+
dynamicRoutes (state) {
4+
return state.dynamicRoutes
5+
},
6+
// used to create recursive aside.
7+
allRoutes (state) {
8+
return state.allRoutes
9+
}
10+
}

src/store/modules/login/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import state from './state'
2+
import getters from './getters'
23
import mutations from './mutations'
34
import actions from './actions'
45

56
export default {
67
namespaced: true,
78
state,
9+
getters,
810
mutations,
911
actions
1012
}

0 commit comments

Comments
 (0)