UNPKG

1.08 kBJavaScriptView Raw
1import _demoList from '.demoList.json';
2import Vue from 'vue/dist/vue.esm';
3import VueRouter from 'vue-router';
4import bus from '@/js/eventbus.js';
5
6import VesselPage from '@/pages/vessel.vue';
7import NotFoundPage from '@/pages/notFound.vue'
8
9Vue.use(VueRouter);
10
11const demoList = _demoList.map((demo) => {
12 if(typeof demo !== 'string') return demo.src;
13 return demo;
14});
15
16const demoRoutes = demoList.map(demoName => ({
17 name: demoName,
18 path: `/${demoName}`,
19 component: VesselPage,
20}));
21
22const defaultRoute = {
23 path: '/',
24 redirect: { name: demoList[0] },
25}
26
27
28const nfpRoute = {
29 path: '/404',
30 component: NotFoundPage,
31}
32
33const unexistRouter = {
34 path: '*',
35 component: NotFoundPage,
36}
37
38const routes = [
39 defaultRoute,
40 ...demoRoutes,
41 nfpRoute,
42 unexistRouter,
43];
44
45
46const router = new VueRouter({
47 routes,
48});
49
50router.beforeEach((to, from, next) => {
51 if(to.name && demoList.indexOf(to.name) > -1) {
52 bus.$emit('setBoxes', to.name);
53 } else if(to.name) {
54 router.push({path: '/404'});
55 }
56 next();
57});
58
59window.router = router;
60export default router;
\No newline at end of file