最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue js separate routes in different files - Stack Overflow

programmeradmin6浏览0评论

I am creating a single page app and i would like to separate the route files in different files

Thta is in my main routing file index.js i have

export default new Router({
 routes: [
   { path: '/', ponent: Hello },
   {path :'/users', ponent: UsersContainerComponent,
       children:[
           {path :'', ponent: UsersComponent},
           {path :'/create', ponent: UsersCreateComponent},
           {path :'/update', ponent: UsersUpdateComponent},

         ]
      },

] })

Now i have other functionalities included not only the users alone hence the code bees huge, How can i outsource the users routing functionality in another file and only include the reference to the main routes file

So how can i achieve something like this

export default new Router({
  routes:[
    {path:'users', .....}// here load users routing in another file

    ]
  })

as this will make the code more readable and manageable.

I am creating a single page app and i would like to separate the route files in different files

Thta is in my main routing file index.js i have

export default new Router({
 routes: [
   { path: '/', ponent: Hello },
   {path :'/users', ponent: UsersContainerComponent,
       children:[
           {path :'', ponent: UsersComponent},
           {path :'/create', ponent: UsersCreateComponent},
           {path :'/update', ponent: UsersUpdateComponent},

         ]
      },

] })

Now i have other functionalities included not only the users alone hence the code bees huge, How can i outsource the users routing functionality in another file and only include the reference to the main routes file

So how can i achieve something like this

export default new Router({
  routes:[
    {path:'users', .....}// here load users routing in another file

    ]
  })

as this will make the code more readable and manageable.

Share Improve this question asked Jul 28, 2017 at 9:44 user8174757user8174757
Add a ment  | 

2 Answers 2

Reset to default 13

I liked disjfa solution: Let’s route a vue app ponent route

import People from '../views/People';
import Wele from './../views/Wele';
import Details from './../views/Details';
import Create from './../views/Create';

export default [
  {
    path: '/people',
    name: 'people',
    ponent: People,
    title: 'People',
    redirect: { name: 'people-wele' },
    children: [
      {
        path: 'wele',
        name: 'people-wele',
        ponent: Wele,
      },
      {
        path: 'create',
        name: 'people-create',
        ponent: Create,
      },
      {
        path: ':id',
        name: 'people-details',
        ponent: Details,
      },
    ],
  },
];

main route

import Vue from 'vue';
import Router from 'vue-router';
import Dashboard from '@/views/Dashboard';
import peopleRoutes from '@/modules/people/router';

Vue.use(Router);

const baseRoutes = [
   ...
];

const routes = baseRoutes.concat(peopleRoutes);
export default new Router({
  routes,
});

You can add children to a route in your main route file.

{
  path: '/events',
  ponent: eventspage,
  children: eventroutes,
},

Inside the child routes ponent you can do the following:

const eventroutes = [
  {
    path: '',
    name: 'eventlist',
    ponent: eventlist,
  },
  {
    path: 'info/:id',
    name: 'eventlist',
    ponent: eventlist,
  },
];

export default eventroutes;

Basically, you can directly take out the routes inside the children part and make them a separate file.

发布评论

评论列表(0)

  1. 暂无评论