Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting routes into separate files

Tags:

vue.js

vuejs2

I am developing a Vue app which is quite big, and I now I have to write all the routes on one page in router/index.js and it is already becoming too long to like or even maintain. The index.js page is full of statements like...

import This from '../components/This'
import That from '../components/That'
import ThatOther from '../components/ThatOther'
import ThatOtherOne from '../components/ThatOtherOne'
// and so on and so on...then at the bottom
var router = new Router({                  
routes: [
    {
        path: '/this', 
        name: 'this', 
        component: This
    },
    {
        path: '/that', 
        name: 'that', 
        component: That
    },
// and so on...so many lines of similar and "repetitive" code

Since my app can be grouped into "modules", is there a way to split the routes into separate files (both the import statements and the router entries) in like router/this.js,router/that.js...', then add them to the main route page,router/index.js`?

like image 840
gthuo Avatar asked Jan 15 '18 14:01

gthuo


People also ask

How do you create separate routes file in node JS Express?

Create Employee Route File in Node First, go to the server folder, create a new folder call it “routes”. Inside the routes folder, create a new file and name it “employees. js” where we specify our Employees Routes. So first we need to import a module that is “express” into the employees.

What are routes files?

The routing file is a VSAM key-sequenced data set that contains information to route each data set to its LPD printer queue. You need one routing file entry (VSAM record) for each printer being used at the installation.

What are Express routers?

The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests. Syntax: express.Router( [options] )

How do you create routes in an express application?

To use the router module in our main app file we first require() the route module (wiki. js). We then call use() on the Express application to add the Router to the middleware handling path, specifying a URL path of 'wiki'.


1 Answers

In a seperate file:

import This from '../components/This'
import That from '../components/That'
import ThatOther from '../components/ThatOther'
import ThatOtherOne from '../components/ThatOtherOne'

export default [
  {
        path: '/this', 
        name: 'this', 
        component: This
    },
    {
        path: '/that', 
        name: 'that', 
        component: That
    },
]

in your route file import the external routes and use the spread oeprator:

import externalRoutesOne from './externalRoutesOne'
import externalRoutesTwo from './externalRoutesTwo'
var router = new Router({                  
routes: [
  ...externalRoutesOne,
  ...externalRoutesTwo
]

Note: the ... operator is required when defining them routes.

like image 160
Philip Feldmann Avatar answered Oct 29 '22 05:10

Philip Feldmann