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

Commit 997cb70

Browse files
committed
feat(router): implement dynamic routes rendering when page reloading
1 parent 9cebe36 commit 997cb70

7 files changed

Lines changed: 52 additions & 29 deletions

File tree

.env.development

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
VUE_APP_BASE_REQUEST=https://easy-mock.com/mock/5bf3bcec941747724a67a53a
1+
VUE_APP_BASE_REQUEST=http://localhost:8800
22
VUE_APP_KEY=development_key
33
VUE_APP_SECRET=development_secret

src/api/routes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export default {
2-
LOGIN: '/login',
3-
DYNAMIC_ROUTES: '/dynamic-routes'
2+
LOGIN: '/user/login',
3+
DYNAMIC_ROUTES: '/user/access'
44
}

src/layout/Material.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@
99
type="text"
1010
@click="onLogout"
1111
>Logout</el-button>
12+
<el-button
13+
class="logout"
14+
type="text"
15+
@click="$router.push('/pages/admin/dashboard')"
16+
>dashboard</el-button>
17+
<el-button
18+
class="logout"
19+
type="text"
20+
@click="$router.push('/pages/admin/table')"
21+
>table</el-button>
22+
<el-button
23+
class="logout"
24+
type="text"
25+
@click="$router.push('/pages/common/user')"
26+
>user info</el-button>
1227
</el-header>
1328

1429
<el-main class="layout__material__placeholder-main">

src/permission/index.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,29 @@ router.beforeEach((to, from, next) => {
2525

2626
// fetching user private routes
2727
if (getTokenFromLocal()) { // User has been logged in
28-
store.dispatch('login/fetchDynamicRoutes')
29-
.then(routes => store.dispatch('login/createGlobalRoutes', routes))
30-
.then(() => next())
31-
.catch(e => {
32-
MessageBox({
33-
title: 'Error',
34-
message: 'We got a error when fetching user access.',
35-
type: 'error'
28+
if (
29+
!store.state.login.dynamicRoutes.length ||
30+
!store.state.login.dynamicRoutes[0].component
31+
) {
32+
store.dispatch('login/fetchDynamicRoutes')
33+
.then(routes => store.dispatch('login/createGlobalRoutes', routes))
34+
.catch(e => {
35+
MessageBox({
36+
title: 'Error',
37+
message: 'We got a error when fetching user access.',
38+
type: 'error',
39+
showClose: false
40+
})
41+
.then(() => store.dispatch('login/userLogout'))
42+
.then(() => next({
43+
path: `/login?redirect=${to.path}`,
44+
replace: true
45+
}))
46+
NProgress.done()
47+
console.error(e)
3648
})
37-
.then(() => next({
38-
path: `/login?redirect=${to.path}`,
39-
replace: true
40-
}))
41-
NProgress.done()
42-
console.error(e)
43-
})
49+
}
50+
next()
4451
} else {
4552
next({
4653
path: `/login?redirect=${to.path}`,

src/router/components/importer.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ export default function createImporters (components) {
2323
* @return {String} A name used to import vue component
2424
*/
2525
export function createChunkName (path) {
26-
const normalizePathSection = path.split('/').map((pathSection, index) => {
27-
return index === 0
28-
? pathSection.replace(/^[A-Z]/, matchKey => matchKey.toLowerCase())
29-
: pathSection.replace(/^[a-z]/, matchKey => matchKey.toUpperCase())
30-
})
31-
return normalizePathSection.join('')
26+
return path.replace(/^\//, '').split('/')
27+
.map((pathSection, index) => {
28+
return index === 0
29+
? pathSection.replace(/^[A-Z]/, matchKey => matchKey.toLowerCase())
30+
: pathSection.replace(/^[a-z]/, matchKey => matchKey.toUpperCase())
31+
})
32+
.join('')
3233
}
3334

3435
/**

src/store/modules/login/actions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export default {
1616
.catch(e => {
1717
if (e.code === 5000) {
1818
vm.$messageBox({
19-
title: '错误',
20-
message: '用户名或密码错误',
19+
title: 'Error',
20+
message: 'Wrong username or password',
2121
type: 'error'
2222
})
2323
}

src/store/modules/login/mutations/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
const components = createImporters(dynamicRoutes)
1919
state.dynamicRoutes = dynamicRoutes.map(path => {
2020
// eg. Routes: {
21-
// path: 'page/dashboard',
21+
// path: '/page/dashboard',
2222
// component: components.pageDashboard
2323
// }
2424
return {
@@ -27,7 +27,7 @@ export default {
2727
}
2828
})
2929
},
30-
[types.SET_ALL_ROUTES] (state, allRoutes) {
31-
state.allRoutes = allRoutes || [...constantRoutes, ...state.dynamicRoutes]
30+
[types.SET_ALL_ROUTES] (state) {
31+
state.allRoutes = [...constantRoutes, ...state.dynamicRoutes]
3232
}
3333
}

0 commit comments

Comments
 (0)