Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hizhengfu/016f51d445f43542a6a3 to your computer and use it in GitHub Desktop.
Save hizhengfu/016f51d445f43542a6a3 to your computer and use it in GitHub Desktop.
// create router
var router = new VueRouter({
linkActiveClass: 'active',
history: false,
saveScrollPosition: true
});
var req = require.context("views", true, /^\.\/.*index\.vue/);
var views = req.keys();
var rules = {}, temp = {};
//递归构建路由算法
_.each(views, function (key) {
var rule = key.match(/^\.\/(.*)\/index\.vue$/)[1],
_rule = rule.split('/'),
_r, _t, _l = _rule[_rule.length - 1];
while (_r = _rule.shift()) {
if (temp[_r] && temp[_r]['component']) {
_t = _r;
continue;
}
if (!temp[_r]) {
temp[_r] = {};
}
if (_t) {
if (!temp[_t]['subRoutes']) {
temp[_t]['subRoutes'] = {};
}
temp[_t]['subRoutes'][_r] = temp[_r];
} else {
rules[_r] = temp[_r];
}
//所有视图都构建为子视图算法
if (_r == _l) {
temp[_r]['component'] = req(key);
/*temp[_r]['component'] = function (resolve) {
require(['views/' + rule + '/index.vue'], resolve)
};*/
}
_t = _r;
}
});
router.map(rules);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment