# convention_based_routing **Repository Path**: evan_origin_admin/convention_based_routing ## Basic Information - **Project Name**: convention_based_routing - **Description**: vue3+ts+vite 使用 import.meta.glob() 方式改造路由,约定式路由; - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-04-07 - **Last Updated**: 2025-04-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # convention_based_routing > vue3+ts+vite项目,约定大于配置,演示:约定式路由 # 使用 import.meta.glob(), 实现约定式路由 ```ts // 约定大于配置,采用约定式路由: /**读取 views 文件夹下所有的 pages.ts 的文件 */ const pages = import.meta.glob('../views/**/pages.ts', { eager: true, // 整个默认 import: 'default', // 获取默认导出结果数据 }) console.log(" pages ", pages) const components = import.meta.glob('../views/**/index.vue') console.log(" components ", components) const routes = Object.entries(pages).map(([path, meta]) => { const comPath = path.replace('pages.ts', 'index.vue') path = path.replace('../views','').replace('/pages.ts', '') || '/' const name = path.split('/').filter(Boolean).join('-') || 'index' console.log("path", path) console.log("name", name) console.log("comPath", comPath) return { path, name, component: components[comPath], meta, } as RouteRecordRaw }) console.log("routes", routes) ```