index.js
1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import Vue from 'vue';
import Router from 'vue-router';
import pages from '../pages';
import _ from 'lodash/core';
Vue.use(Router);
const loadRoutes = (rous, paths, children) => {
if (_.has(rous, 'path')) {
let ps = _.flattenDeep(paths).filter(p => p);
if (_.last(ps) === rous.name) {
ps.splice(ps.length - 1, 1);
}
if (!children) {
if (rous.path) {
rous.path = '/' + ps.join('/') + (rous.path[0] === '/' ? '' : '/') + rous.path;
} else {
rous.path = ps.join('/') + '.html';
}
}
rous.name = _.concat(ps, [rous.name]).join('.');
if (rous.children) {
_.each(rous.children, child => loadRoutes(child, [paths, child.name], true));
return [rous];
}
return [rous];
}
if (rous.length) {
return _.map(rous, r => {
return loadRoutes(r, [paths]);
});
} else {
return _.map(rous, (rou, k) => {
return loadRoutes(rou, [paths, k]);
});
}
};
const routes = _.flattenDeep(loadRoutes(pages));
export function createRouter() {
return new Router({
mode: 'history',
routes,
scrollBehavior(to, from, savedPosition) {
if (savedPosition) {
return savedPosition;
} else {
return { x: 0, y: 0 };
}
}
});
}