1 | # vue-route-generator
|
2 |
|
3 | [Vue Router](https://github.com/vuejs/vue-router) route config generator.
|
4 |
|
5 | You may want to use [vue-auto-routing](https://github.com/ktsn/vue-auto-routing) for auto generating routing or [vue-cli-plugin-auto-routing](https://github.com/ktsn/vue-cli-plugin-auto-routing) which includes all useful features on routing.
|
6 |
|
7 | ## Overview
|
8 |
|
9 | This tool generates a JavaScript code that exporting Vue Router's `routes` config by reading your Vue components directory.
|
10 |
|
11 | For example, when you have following file structure:
|
12 |
|
13 | ```
|
14 | pages/
|
15 | ├── index.vue
|
16 | ├── users.vue
|
17 | └── users/
|
18 | └── _id.vue
|
19 | ```
|
20 |
|
21 | Then run the following script:
|
22 |
|
23 | ```js
|
24 | const { generateRoutes } = require('vue-route-generator')
|
25 |
|
26 | const code = generateRoutes({
|
27 | pages: './pages' // Vue page component directory
|
28 | })
|
29 |
|
30 | console.log(code)
|
31 | ```
|
32 |
|
33 | vue-route-generator will generate like the following code (beautified the indentations etc.):
|
34 |
|
35 | ```js
|
36 | export default [
|
37 | {
|
38 | name: 'index',
|
39 | path: '/',
|
40 | component: () => import('@/pages/index.vue')
|
41 | },
|
42 | {
|
43 | name: 'users',
|
44 | path: '/users',
|
45 | component: () => import('@/pages/users.vue'),
|
46 | children: [
|
47 | {
|
48 | name: 'users-id',
|
49 | path: ':id',
|
50 | component: () => import('@/pages/users/_id.vue')
|
51 | }
|
52 | ]
|
53 | }
|
54 | ]
|
55 | ```
|
56 |
|
57 | You can save the code and include router instance:
|
58 |
|
59 | ```js
|
60 | const fs = require('fs')
|
61 | const { generateRoutes } = require('vue-route-generator')
|
62 |
|
63 | const code = generateRoutes({
|
64 | pages: './pages'
|
65 | })
|
66 |
|
67 | fs.writeFileSync('./router/routes.js', code)
|
68 | ```
|
69 |
|
70 | ```js
|
71 | // router/index.js
|
72 | import Vue from 'vue'
|
73 | import Router from 'vue-router'
|
74 |
|
75 | // import generated routes
|
76 | import routes from './routes'
|
77 |
|
78 | Vue.use(Router)
|
79 |
|
80 | export default new Router({
|
81 | routes
|
82 | })
|
83 | ```
|
84 |
|
85 | ## Routing
|
86 |
|
87 | The routing is inspired by [Nuxt routing](https://nuxtjs.org/guide/routing) and is implemented with the same functionality.
|
88 |
|
89 | ### Partials
|
90 |
|
91 | Directories and files started and ended with `__` (two underscores, e.g. `__foo__.vue`) will be ignored. You can use them as partial components which will be used in some page components.
|
92 |
|
93 | ## Route Custom Block
|
94 |
|
95 | If the components have `<route>` custom block, its json content will be merged into the generated route record.
|
96 |
|
97 | For example, if `index.vue` has the following `<route>` block:
|
98 |
|
99 | ```vue
|
100 | <route>
|
101 | {
|
102 | "name": "home",
|
103 | "meta": {
|
104 | "requiresAuth": true
|
105 | }
|
106 | }
|
107 | </route>
|
108 |
|
109 | <template>
|
110 | <h1>Hello</h1>
|
111 | </template>
|
112 | ```
|
113 |
|
114 | The generated route config is like following:
|
115 |
|
116 | ```js
|
117 | module.exports = [
|
118 | {
|
119 | name: 'home', // Overwritten by <route> block's name field.
|
120 | path: '/',
|
121 | component: () => import('@/pages/index.vue'),
|
122 |
|
123 | // Added by <route> block's meta field.
|
124 | meta: {
|
125 | requiresAuth: true
|
126 | }
|
127 | }
|
128 | ]
|
129 | ```
|
130 |
|
131 | ## Syntax Highlighting
|
132 |
|
133 | To enable syntax highlighting in VS Code using [Vetur's Custom Code Blocks](https://vuejs.github.io/vetur/highlighting.html#custom-block) add the following snippet to your preferences...
|
134 | ```
|
135 | "vetur.grammar.customBlocks": {
|
136 | "route": "json"
|
137 | }
|
138 | ```
|
139 |
|
140 | ## References
|
141 |
|
142 | ### `generateRoutes(config: GenerateConfig): string`
|
143 |
|
144 | `GenerateConfig` has the following properties:
|
145 |
|
146 | - `pages`: Path to the directory that contains your page components.
|
147 | - `importPrefix`: A string that will be added to importing component path (default `@/pages/`).
|
148 | - `dynamicImport`: Use dynamic import expression (`import()`) to import component (default `true`).
|
149 | - `chunkNamePrefix`: Prefix for each route chunk name (only available when `dynamicImport: true`).
|
150 | - `nested`: If `true`, generated route path will be always treated as nested. (e.g. will generate `path: 'foo'` rather than `path: '/foo'`)
|
151 |
|
152 | ## Related Projects
|
153 |
|
154 | - [vue-cli-plugin-auto-routing](https://github.com/ktsn/vue-cli-plugin-auto-routing): Vue CLI plugin including auto pages and layouts resolution.
|
155 | - [vue-router-layout](https://github.com/ktsn/vue-router-layout): Lightweight layout resolver for Vue Router.
|
156 | - [vue-auto-routing](https://github.com/ktsn/vue-auto-routing): Generate Vue Router routing automatically.
|
157 |
|
158 | ## License
|
159 |
|
160 | MIT
|