Giter Club home page Giter Club logo

vue-route-generator's Introduction

vue-route-generator

Vue Router route config generator.

You may want to use vue-auto-routing for auto generating routing or vue-cli-plugin-auto-routing which includes all useful features on routing.

Overview

This tool generates a JavaScript code that exporting Vue Router's routes config by reading your Vue components directory.

For example, when you have following file structure:

pages/
├── index.vue
├── users.vue
└── users/
    └── _id.vue

Then run the following script:

const { generateRoutes } = require('vue-route-generator')

const code = generateRoutes({
  pages: './pages' // Vue page component directory
})

console.log(code)

vue-route-generator will generate like the following code (beautified the indentations etc.):

export default [
  {
    name: 'index',
    path: '/',
    component: () => import('@/pages/index.vue')
  },
  {
    name: 'users',
    path: '/users',
    component: () => import('@/pages/users.vue'),
    children: [
      {
        name: 'users-id',
        path: ':id',
        component: () => import('@/pages/users/_id.vue')
      }
    ]
  }
]

You can save the code and include router instance:

const fs = require('fs')
const { generateRoutes } = require('vue-route-generator')

const code = generateRoutes({
  pages: './pages'
})

fs.writeFileSync('./router/routes.js', code)
// router/index.js
import Vue from 'vue'
import Router from 'vue-router'

// import generated routes
import routes from './routes'

Vue.use(Router)

export default new Router({
  routes
})

Routing

The routing is inspired by Nuxt routing and is implemented with the same functionality.

References

generateRoutes(config: GenerateConfig): string

GenerateConfig has the following properties:

  • pages: Path to the directory that contains your page components.
  • importPrefix: A string that will be added to importing component path (default @/pages/).
  • dynamicImport: Use dynamic import expression (import()) to import component (default true).

Related Projects

License

MIT

vue-route-generator's People

Contributors

ktsn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.