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

Commit edb9a86

Browse files
committed
feat(router): components importer implemented
1 parent 13d96c7 commit edb9a86

6 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
export default {
1313
data () {
1414
return {
15-
layout: 'Login'
15+
layout: 'material' // Default layout file name
1616
}
1717
},
1818

src/layout/Material.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<el-container>
66
<el-header>header</el-header>
7-
<el-main>main</el-main>
7+
<router-view/>
88
<el-footer>footer</el-footer>
99
</el-container>
1010
</el-container>

src/router/components/constant.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// ! constant component path, based on `/src` directory
2+
export default [
3+
'layout/Login',
4+
'layout/Material',
5+
'pages/Home'
6+
]

src/router/components/importer.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default function createImporters (components) {
2+
const importers = {}
3+
components.forEach(path => {
4+
const chunkName = createChunkName(path)
5+
if (!importers[chunkName]) {
6+
importers[chunkName] = () => import(
7+
/* webpackChunkName: 'async/[request][index]' */
8+
`SOURCE/${path}`
9+
)
10+
}
11+
})
12+
return importers
13+
}
14+
15+
function createChunkName (path) {
16+
const normalizePathSection = path.split('/').map((pathSection, index) => {
17+
return index === 0
18+
? pathSection.replace(/^[A-Z]/, matchKey => matchKey.toLowerCase())
19+
: pathSection.replace(/^[a-z]/, matchKey => matchKey.toUpperCase())
20+
})
21+
return normalizePathSection.join('')
22+
}

src/router/components/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import createImporters from './importer'
2+
import constant from './constant'
3+
4+
export const constantComponents = createImporters(constant)

src/router/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Vue from 'vue'
22
import Router from 'vue-router'
3+
import { constantComponents } from './components'
34

45
Vue.use(Router)
56

@@ -8,7 +9,7 @@ export default new Router({
89
{
910
path: '/',
1011
name: 'home',
11-
component: () => import(/* webpackChunkName: 'home' */ `PAGES/Home`)
12+
component: constantComponents.pagesHome
1213
}
1314
]
1415
})

0 commit comments

Comments
 (0)