当前位置: 首页 > news >正文

Vue工程化实现约定式路由自动注册

获取编译时态的目录结构方式:

  • webpack工程:     require.context()

  • vite工程:     import.meta.glob()

约定式路由自动注册(vite)

// router/index.js 路由模块import { createRouter, createWebHistory } from "vue-router";const modules = import.meta.glob("../views/**/index.vue");const autoRoutes = Object.keys(modules).map((path) => {const name = path.match(/\.\.\/views\/(.*?)\/index\.vue$/)[1];return {path: `/${name}`,component: modules[path],name: name.replace(/\//g, "-"), // 处理多级目录};
});// 手动配置部分
const manualRoutes = [{path: "/",name: "home",component: () => import("../views/HomeView.vue"),meta: { requiresAuth: false },},// {//   path: "/:pathMatch(.*)*",//   component: () => import("@/views/404/index.vue"),// },
];export default createRouter({history: createWebHistory(),routes: [...manualRoutes, ...autoRoutes], // 合并路由
});

目录结构 转 对象结构 (vite工程)

采用 约定大于配置 的方式获取目录结构

应用示例:

  • 需求

在这里插入图片描述

  • 目录结构

在这里插入图片描述

  • n1/n2文件:每个JS文件中的输出语句对应各自的文件名
    在这里插入图片描述
  • 实现
// 根目录: m.js// webpack 获取编译时态的目录结构: require.context//vite 获取编译时态的目录结构: import.meta.glob/** 获取bar目录下所有js模块*  return: key: 模块路径,  value: 动态导入语句*  ./TempTestModule/bar/a/n1.js:  () => import("/TempTestModule/bar/a/n1.js")*/
// const modules = import.meta.glob("./TempTestModule/bar/**/*.js");// 如果想同时获取bar目录和foo目录下所有的JS模块,可以用数组的形式
const modules = import.meta.glob(["./TempTestModule/bar/**/*.js", "./TempTestModule/foo/**/*.js"],{eager: true, // 不再使用动态导入,直接获取模块的导入结果import: "default", // 获取默认导出结果}
);
// console.log(modules);const result = {};for (const path in modules) {const moduleDefault = modules[path];// 去除路径中的 /反斜杠、 .点,以及删除数组中的第一项TempTestModule(顶端文件夹名)和最后一项 .js(文件后缀名)const matches = path.match(/[^\/\.]+/g).slice(1, -1);// console.log(path, matches);let current = result;for (let i = 0; i < matches.length; i++) {const key = matches[i];current[key] = current[key] || {};// 如果是数组的最后一项,直接赋值为导出结果if (i === matches.length - 1) {current[key] = moduleDefault;}current = current[key];}
}
// console.log(result);export default result;
// 根目录: index.html执行调用 
<script type="module">import M from "./m.js";// 确保DOM加载完成后再执行document.addEventListener("DOMContentLoaded", () => {// 用 约定大于配置的方式在编译时态获取目录结构// 把TempTestModule目录下的整个 目录结构 转换为 对象结构 并输出打印语句M.bar.a.n1.m(); // bar/a/n1.jsM.bar.a.n2.m(); // bar/a/n2.jsM.bar.b.n1.m(); // bar/b/n1.jsM.bar.b.n2.m(); // bar/b/n2.jsM.foo.a.n1.m(); // foo/a/n1.jsM.foo.a.n2.m(); // foo/a/n2.jsM.foo.b.n1.m(); // foo/b/n1.jsM.foo.b.n2.m(); // foo/b/n2.js});
</script>

文章转载自:
http://altaic.dxwdwl.cn
http://caustic.dxwdwl.cn
http://cahoots.dxwdwl.cn
http://blacking.dxwdwl.cn
http://castigatory.dxwdwl.cn
http://autecologic.dxwdwl.cn
http://atmolyze.dxwdwl.cn
http://blacklead.dxwdwl.cn
http://choirgirl.dxwdwl.cn
http://barbican.dxwdwl.cn
http://anglic.dxwdwl.cn
http://catastrophic.dxwdwl.cn
http://breath.dxwdwl.cn
http://amino.dxwdwl.cn
http://bes.dxwdwl.cn
http://algraphy.dxwdwl.cn
http://aspergill.dxwdwl.cn
http://aunty.dxwdwl.cn
http://cherup.dxwdwl.cn
http://amperehour.dxwdwl.cn
http://beng.dxwdwl.cn
http://agist.dxwdwl.cn
http://arcature.dxwdwl.cn
http://barothermogram.dxwdwl.cn
http://ahemeral.dxwdwl.cn
http://ceres.dxwdwl.cn
http://britannia.dxwdwl.cn
http://antipode.dxwdwl.cn
http://boddhisattva.dxwdwl.cn
http://billboard.dxwdwl.cn
http://www.dtcms.com/a/261747.html

相关文章:

  • Go开发工程师-Golang基础知识篇
  • 数据结构:最小生成树—Prim(普里姆)与Kruskal(克鲁斯卡尔)算法
  • 什么是哈希链(Hash Chain)?
  • Redis 实现分布式锁
  • 分布式集群压测
  • 第8章-财务数据
  • VR训练美国服务器:高性能解决方案与优化指南
  • 培养你的眼力 - 摄影构图
  • Python 中 `for` 循环与 `while` 循环的性能差异:深度剖析
  • 思维提升篇-数学抽象与计算机实现
  • 从零开始构建RAG(检索增强生成)
  • C# 委托(为委托添加方法和从委托移除方法)
  • Excel限制编辑:保护表格的实用功能
  • 【C#】使用电脑的GPU与CPU的区别
  • 鸿蒙5:其他布局容器
  • 【Redis原理】Redis分布式缓存——主从复制、哨兵机制与Redis Cluster
  • deepin 25 交换 caps lctl
  • Lua现学现卖
  • SpringBoot项目使用arthas-tunnel-server
  • AtCoder AT_abc412_c [ABC412C] Giant Domino 题解
  • 【力扣 简单 C】121. 买卖股票的最佳时机
  • GitHub Actions 实现 AWS ECS 服务的多集群安全重启方案
  • 【AI实践】Mac一天熟悉AI模型智能体应用(百炼版)
  • STM32中Usart的使用
  • 一个简单测试Deepseek吞吐量的脚本,国内环境可跑
  • 1.1 基于Icarus Verilog、ModelSim和Vivado对蜂鸟E203处理器进行仿真
  • HarmonyOS File和base64字符串转换
  • Note2.2 机器学习训练技巧:Batch and Momentum(Machine Learning by Hung-yi Lee)
  • C语言二级指针与多级指针
  • cannot import name ‘TextKwargs‘ from ‘transformers.processing_utils‘