import.meta.glob 自动读取路由

src/router/index.js

import { createRouter, createWebHistory } from 'vue-router'
const initRouteModules = async () => {
  // vite提供的批量导入文件方法,导入模块路由
  // 需要在应用加载时就预先加载所有匹配到的模块,以便加快应用的启动速度{ eager: true },可能导致初始加载慢,按需开启
  const routeModules = import.meta.glob('../views/*/routes.js', { eager: false })

  const routes = await Promise.all(
    Object.keys(routeModules).map(async (path) => {
      const routeModule = await routeModules[path]()
      return routeModule.default
    })
  )

  return routes.flat()
}

// 初始化模块路由
const moduleRoutes = initRouteModules()

// 等待模块路由初始化完成
const routes = await moduleRoutes

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home',
    },
    {
      path: '/home',
      name: 'Home',
      component: () => import('../views/home/index.vue')
    },

    // 添加模块路由
    ...routes
  ]
})

export default router

src/main.js

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

const app = createApp(App)

app.use(router)

app.mount('#app')

src/views/home/routes.js

const initRouteModules = async () => {
  // vite提供的批量导入文件方法,导入模块路由
  // 需要在应用加载时就预先加载所有匹配到的模块,以便加快应用的启动速度{ eager: true },可能导致初始加载慢,按需开启
  const routeModules = import.meta.glob('./subPages/*/route.ts', { eager: false })

  const routes = await Promise.all(
    Object.keys(routeModules).map(async (path) => {
      const routeModule = await routeModules[path]()
      return routeModule.default
    })
  )

  return routes.flat()
}

// 初始化模块路由
const moduleRoutes = initRouteModules()

// 等待模块路由初始化完成
const routes = await moduleRoutes

export default [
  {
    path: '/home',
    name: 'Home',
    component: () => import('./index.vue'),
    meta: {
      title: '首页'
    },
    children: routes
  }
]

src/views/myWorkbench/routes.js

const initRouteModules = async () => {
  // vite提供的批量导入文件方法,导入模块路由
  // 需要在应用加载时就预先加载所有匹配到的模块,以便加快应用的启动速度{ eager: true },可能导致初始加载慢,按需开启
  const routeModules = import.meta.glob('./subPages/route.js', { eager: false })

  const routes = await Promise.all(
    Object.keys(routeModules).map(async (path) => {
      const routeModule = await routeModules[path]()
      return routeModule.default
    })
  )

  return routes.flat()
}

// 初始化模块路由
const moduleRoutes = initRouteModules()

// 等待模块路由初始化完成
const routes = await moduleRoutes

export default [
  {
    path: '/myWorkbench',
    name: 'MyWorkbench',
    component: () => import('./index.vue'),
    meta: {
      title: '我的工作台'
    },
    children: routes
  }
]

src/views/myWorkbench/subPages/route.js

export default [
  {
    path: '/score_query',
    name: 'score_query',
    component: () => import('./scoreInquiry/index.vue'),
    meta: {
      title: '成绩查询'
    },
  },
  {
    path: '/score_import',
    name: 'score_import',
    component: () => import('./scoreImport/index.vue'),
    meta: {
      title: '成绩导入'
    },
  }
]

项目目录结构

在这里插入图片描述

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于使用 import.meta.glob 封装路由,你可以按照以下步骤进行操作: 1. 创建一个文件夹用于存放路由模块文件,比如名为 "routes" 的文件夹。在该文件夹下创建各个路由模块文件,每个文件代表一个路由模块。 2. 在主文件(比如 app.js 或 index.js)中使用 import.meta.glob 方法来动态导入这些路由模块文件。import.meta.glob 是一个异步函数,它可以根据模式匹配自动导入多个文件。 ```javascript // app.js 或 index.js const routes = import.meta.glob('./routes/*.js') Object.entries(routes).forEach(([path, module]) => { const routePath = path.replace('./routes', '').replace('.js', '') const router = module.default || module // 在这里使用 router 对应的路由模块进行处理 // 可以根据 routePath 定义路由路径或其他操作 }) ``` 上述代码中,import.meta.glob('./routes/*.js') 表示将匹配所有以 .js 结尾的文件,并返回一个对象,其中键名为文件路径,键值为导入的模块。 3. 在每个路由模块文件中,导出一个 Express 路由对象。你可以在每个路由模块中定义该路由模块所处理的具体路由路径和对应的处理函数。 ```javascript // exampleRoute.js const express = require('express') const router = express.Router() // 定义具体的路由和处理函数 router.get('/', (req, res) => { // 处理请求并返回响应 }) // 导出路由对象 module.exports = router ``` 4. 使用 import.meta.glob 方法动态导入路由模块后,你可以在适当的位置使用导出的 router 对象来处理路由请求。 这样,你就可以使用 import.meta.glob 方法封装路由,通过动态导入多个路由模块来管理和处理不同的路由路径。根据需要,你还可以在处理函数中进行其他操作,比如验证用户身份或调用其他中间件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值