• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
技术杨的博客园
希望每一次的努力都能得到自己想要的
博客园    首页    新随笔    联系   管理    订阅  订阅

项目优化router路由自动化引入功能

function importRoutes() {
  const routes = [];
  const context = require.context('./modules', true, /\.js$/); // 指定要导入的目录和文件匹配规则

  context.keys().forEach((fileName) => {
    const routeConfig = context(fileName);
    routes.push(routeConfig.default || routeConfig); // 支持默认导出和命名导出
  });

  return routes;
}
const routesArr = []
const routesList = importRoutes()
for (let i = 0; i < routesList.length; i++) {
  for (const routerKey in routesList[i]) {
    routesArr.push(routesList[i][routerKey])
  }
}

 

posted @ 2023-06-29 09:16  技术杨  阅读(32)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3