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`?
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.
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.
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] )
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'.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With