UNPKG

1.07 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') demo = 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
27const nfpRoute = {
28 path: '/404',
29 component: NotFoundPage
30};
31
32const unexistRouter = {
33 path: '*',
34 component: NotFoundPage
35};
36
37const routes = [defaultRoute, ...demoRoutes, nfpRoute, unexistRouter];
38
39const router = new VueRouter({
40 routes
41});
42
43router.beforeEach((to, from, next) => {
44 if (to.name && demoList.indexOf(to.name) > -1) {
45 bus.$emit('setBoxes', to.name);
46 } else if (to.name) {
47 router.push({ path: '/404' });
48 }
49 next();
50});
51
52window.router = router;
53export default router;