yoho-plugin-router.js
1.55 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
56
57
58
59
60
61
62
63
import Router from 'vue-router';
import pageRoutes from '../pages';
import layout from '../pages/layout';
import common from '../pages/common';
import _ from 'lodash';
export default {
loadRouters(rous, paths, children) {
if (_.has(rous, 'path')) {
let ps = _.flattenDeep(paths).filter(p => p);
if (_.last(ps) === rous.name) {
ps = _.dropRight(ps);
}
if (!children) {
if (rous.path) {
rous.path = _.join(ps, '/') + (rous.path[0] === '/' ? '' : '/') + rous.path;
} else {
rous.path = _.join(ps, '/') + '.html';
}
}
rous.name = _.join(_.concat(ps, [rous.name]), '.');
if (rous.children) {
_.each(rous.children, child => this.loadRouters(child, [paths, child.name], true));
return [rous];
}
return [rous];
}
if (rous.length) {
return _.map(rous, r => {
return this.loadRouters(r, [paths]);
});
} else {
return _.map(rous, (rou, k) => {
return this.loadRouters(rou, [paths, k]);
});
}
},
install(Vue) {
const childRouters = _.flattenDeep(this.loadRouters(pageRoutes));
if (Vue.$config.homePage) {
const homePage = _.find(childRouters, router => router.name === Vue.$config.homePage);
homePage && (homePage.path = '/');
}
let routes = [
{
path: '/',
component: layout,
children: childRouters,
},
];
routes = routes.concat(common);
Vue.$router = new Router({
routes,
mode: Vue.$config.historyMode,
});
},
};