You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Choose a package manager you like.
npm install vite-vue-route-generator --save
# or
yarn add vite-vue-route-generator
# or
pnpm install vite-vue-route-generator
Usage
Example of src/views/:
├─index.vue
└─foo
├─ app.tsx
└─ index.vue
Generate route
// file: src/router/index.tsimport{createRouter,createWebHistory}from"vue-router";import{getRoutes}from"vite-vue-route-generator";constrouter=createRouter({history: createWebHistory(import.meta.env.BASE_URL),// 👇 Use globe syntax to retrieve all files in the page view directoryroutes: getRoutes(import.meta.glob("../views/**/**.vue"),{/** * Required. The root path of the directory tree, using relative paths, ending with '/' */pathRoot: "../views/",debugger: true,// To print the route.}),});exportdefaultrouter;
Result
// Based on the routing configuration generated from the directory above:[{"path": "/foo","children": [{"name": "FooApp","path": "app","component": ()=>import('../views/foo/app.tsx')},{"name": "FooIndex","path": "","component": ()=>import('../views/foo/index.vue')}]},{"name": "Index","path": "/","component": ()=>import('../views/index.vue')}]
Naming rules
Homepage filename is HomeView.vue or Index.uve, index.vue (Must)
NotFound page is 404.vue or notfound.vue, NotFound.vue (Must)
Support file formats
.vue
.tsx
.jsx
Dynamic Route Matching with Params
Parameter
src/views/User/list-[pid]-[userName].vue (File)
→ /User/list-:pid-:userName (Vue route configure parameter of path)