1 | import _demoList from '.demoList.json';
|
2 | import Vue from 'vue/dist/vue.esm';
|
3 | import VueRouter from 'vue-router';
|
4 | import bus from '@/js/eventbus.js';
|
5 |
|
6 | import VesselPage from '@/pages/vessel.vue';
|
7 | import NotFoundPage from '@/pages/notFound.vue';
|
8 |
|
9 | Vue.use(VueRouter);
|
10 |
|
11 | const demoList = _demoList.map(demo => {
|
12 | if (typeof demo !== 'string') demo = demo.src;
|
13 | return demo;
|
14 | });
|
15 |
|
16 | const demoRoutes = demoList.map(demoName => ({
|
17 | name: demoName,
|
18 | path: `/${demoName}`,
|
19 | component: VesselPage
|
20 | }));
|
21 |
|
22 | const defaultRoute = {
|
23 | path: '/',
|
24 | redirect: { name: demoList[0] }
|
25 | };
|
26 |
|
27 | const nfpRoute = {
|
28 | path: '/404',
|
29 | component: NotFoundPage
|
30 | };
|
31 |
|
32 | const unexistRouter = {
|
33 | path: '*',
|
34 | component: NotFoundPage
|
35 | };
|
36 |
|
37 | const routes = [defaultRoute, ...demoRoutes, nfpRoute, unexistRouter];
|
38 |
|
39 | const router = new VueRouter({
|
40 | routes
|
41 | });
|
42 |
|
43 | router.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 |
|
52 | window.router = router;
|
53 | export default router;
|