增加 data-route 支持

This commit is contained in:
邹景立 2021-11-29 22:07:48 +08:00
parent a73be5a0c4
commit 7f315f95bc
2 changed files with 31 additions and 20 deletions

View File

@ -30,28 +30,27 @@
routes: [], history: VueRouter.createWebHashHistory(),
});
// 添加默认路由
router.addRoute({path: '/', redirect: '/static/template/pages/one.vue'});
// 路由前置处理
router.beforeEach(function (to, fr, next) {
let page = to.fullPath;
if (to.fullPath === '/') {
page = './static/template/pages/one.vue';
}
page = page.replace(/-/g, '/');
const name = page.replace(/[.\/]+/g, '_');
let name = to.fullPath.replace(/[.\/]+/g, '_');
if (router.hasRoute(name)) {
next();
} else {
router.addRoute({name: name, path: to.fullPath, component: loadVueFile(page)});
// component: Vue.defineAsyncComponent(() => loadVue(page))
router.addRoute({name: name, path: to.fullPath, component: loadVueFile(to.fullPath)});
next({name: name});
}
});
// 动态注销路由
router.afterEach(function (to) {
console.log('Route: ', to.name);
if (router.hasRoute(to.fullPath)) {
router.removeRoute(to.fullPath)
let name = to.fullPath.replace(/[.\/]+/g, '_');
if (router.hasRoute(name)) {
console.log('clear', name)
router.removeRoute(name)
}
});
@ -62,14 +61,13 @@
// }
// });
window.$think = Vue.createApp(Vue.defineAsyncComponent(() => loadVue('./static/template/layout.vue')));
const app = Vue.createApp(Vue.defineAsyncComponent(() => loadVue('./static/template/layout.vue')));
// 全局字体文件
// const icons = await loadVue("https://unpkg.com/@element-plus/icons@0.0.11/lib/index.js");
// for (let i in icons) window.$think.component(i, icons[i]);
// for (let i in icons) app.component(i, icons[i]);
window.$think.use(router).use(ElementPlus).mount(document.body);
app.use(router).use(ElementPlus).mount(document.body);
})().catch(function (ex) {
console.error(ex);

View File

@ -46,7 +46,7 @@
</el-tooltip>
</template>
<h5 v-text="menu.title"></h5>
<el-menu :default-active="1" :open="1" :router="true">
<el-menu :default-active="1" :open="1">
<el-sub-menu index="1">
<template #title>
<!--<el-icon>-->
@ -55,9 +55,9 @@
<span>Navigator One</span>
</template>
<el-menu-item index="1-1" route="static-template-pages-one.vue">item one route</el-menu-item>
<el-menu-item index="1-2" route="static-template-pages-two.vue">item two route</el-menu-item>
<el-menu-item index="1-3" route="static-template-pages-thr.vue">item thr route</el-menu-item>
<el-menu-item index="1-1" data-route="/static/template/pages/one.vue">item one route</el-menu-item>
<el-menu-item index="1-2" data-route="/static/template/pages/two.vue">item two route</el-menu-item>
<el-menu-item index="1-3" data-route="/static/template/pages/thr.vue">item thr route</el-menu-item>
</el-sub-menu>
<el-menu-item index="2">
@ -294,6 +294,19 @@ export default {
{name: '199', title: 'SHOW-FOR-LIST', subs: []},
]
}
},
created() {
let app = this;
// data-route
document.addEventListener('click', function (event) {
event.path.some(function (ele) {
if (ele.dataset && ele.dataset.route) {
app.$router.push(ele.dataset.route);
return true;
}
});
})
}
}
</script>