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

vue-pure-admin动态路由无Layout实现解决方案

背景:

最近在使用vue-pure-admin开发后台项目的时候发现作者并没有动态路由的全屏无Layout实现方案。查询作者路由发现,作者只做了静态路由的无Layout方案,其它动态路由,作者在做整合的时候,都放进了 \ 下面的子路由,\ 即Layout路由。于是我开启了如何修改路由生成,来支持动态路由的无Layout实现

步骤一、定义路由modules文件

例如为不需要Layout的路由添加 isLayout:true,同时针对特定页面的子路由添加 showNoLayoutComponent:true,解决组件查找到非index.vue文件的错误问题。

export default {path: "/statistics",name: "StatisticAnalysis",redirect: "/statistics/large_screen",meta: {icon: "changzhanguanli2",title: "统计分析",isLayout: true,rank: 8},children: [{path: "/statistics/large_screen",name: "RealTimeScreen",redirect: "/statistics/large_screen/real_time_screen",meta: {title: "实时大屏",isLayout: true},auth: true,children: [{path: "/statistics/large_screen/real_time_screen",name: "RealTimeScreen",component: () =>import("@/views/statistics/large_screen/real_time_screen/Home.vue"),auth: true,meta: {title: "实时大屏",isLayout: true,showNoLayoutComponent: true}}]}]
} as RouteConfigsTable;

步骤二、处理动态路由生成

a.找到做做生成routes的地方 函数 router\utils.ts 的函数 handleAsyncRoutes 作者在此处进行了路由的添加,导致全部都被添加进Layout下面,导致无法在Layout之外

function handleAsyncRoutes(routeList) {if (routeList.length === 0) {usePermissionStoreHook().handleWholeMenus(routeList);} else {function filterRoutes(routes, permissions) {return routes.reduce((acc, route) => {// 过滤当前路由if (!route.auth) {const filteredRoute = cloneDeep(route); // 复制当前路由// 如果有子路由,则递归过滤子路由if (route.children) {filteredRoute.children = filterRoutes(route.children, permissions);}acc.push(filteredRoute);} else if (route.name && permissions.includes(route.name)) {const filteredRoute = { ...route }; // 复制当前路由// 如果有子路由,则递归过滤子路由if (route.children) {filteredRoute.children = filterRoutes(route.children, permissions);}acc.push(filteredRoute);}return acc;}, []);}formatFlatteningRoutes(addAsyncRoutes(hasPermissionPage)).map((v: RouteRecordRaw) => {// 防止重复添加路由if (router.options.routes[0].children.findIndex(value => value.path === v.path) !== -1) {return;} else {router.options.routes[0].children.push(v);ascending(router.options.routes[0].children);if (!router.hasRoute(v?.name)) router.addRoute(v);const flattenRouters: any = router.getRoutes().find(n => n.path === "/");router.addRoute(flattenRouters);}});usePermissionStoreHook().handleWholeMenus(hasPermissionPage);}if (!useMultiTagsStoreHook().getMultiTagsCache) {useMultiTagsStoreHook().handleTags("equal", [...routerArrays,...usePermissionStoreHook().flatteningRoutes.filter(v => v?.meta?.fixedTag)]);}addPathMatch();
}

 b.我们只需将对于的无Layout文件处理一下,单独加入routes就行,将else中代码进行改造即可

else {/* router.options.routes[0].children.push(v);// 最终路由进行升序ascending(router.options.routes[0].children);if (!router.hasRoute(v?.name)) router.addRoute(v);const flattenRouters: any = router.getRoutes().find(n => n.path === "/");router.addRoute(flattenRouters); */// 切记将路由push到routes后还需要使用addRoute,这样路由才能正常跳转if (!v.meta?.isLayout) {// 切记将路由push到routes后还需要使用addRoute,这样路由才能正常跳转router.options.routes[0].children.push(v);// 最终路由进行升序ascending(router.options.routes[0].children);if (!router.hasRoute(v?.name)) router.addRoute(v);const flattenRouters: any = router.getRoutes().find(n => n.path === "/");router.addRoute(flattenRouters);} else {if (v.meta.showNoLayoutComponent) {router.options.routes.splice(1, 0, v);if (!router.hasRoute(v?.name)) router.addRoute(v);}}}}

c.现在就可以进行无Layout展示了

http://www.xdnf.cn/news/607393.html

相关文章:

  • 【大数据】宽表和窄表
  • Python 实现基于 OpenAI API 的文章标题自动生成评论
  • ASUS华硕ROG枪神9P笔记本G815LP(G615LW,G635LR,G835LX)原装出厂Win11系统,开箱状态oem系统
  • 【趣味Python】第8课:小汽车游戏
  • 在公司快速查看与固定内网IP地址的完整指南
  • SpringAI(GA版)的Advisor:快速上手+源码解读
  • ProfiNet转Ethernet/IP网关选型策略适配西门子S7-1500与罗克韦尔ControlLogix5580的关键指标对比
  • 架构师论文《论软件可靠性模型的设计与实现》
  • 画思维导图的方法分享
  • 镭神N10P SLAM算法选型
  • 《进化陷阱》--AI 生成文章 《连载 2》
  • Java Lock使用
  • 安全运营与威胁对抗体系
  • 【分布式文件系统】FastDFS
  • 语音合成之十五 语音合成(TTS)分句生成拼接时的响度一致性问题:现状、成因与对策
  • 拉普拉斯算子过零点边缘检测原理以及抑制伪边缘的方法
  • 农业机械化、电气化和自动化知网英文普刊:1天录用,2周见刊发表!
  • 全链路解析:影刀RPA+Coze API自动化工作流实战指南
  • 静态时序分析与约束
  • Python 和 matplotlib 保存图像时,确保图像的分辨率和像素符合特定要求(如 64x64),批量保存 不溢出内存
  • 单机Kafka配置ssl并在springboot使用
  • 【android bluetooth 协议分析 02】【bluetooth hal 层详解 4】【高通蓝牙hal主要流程介绍-中】
  • 26考研|高等代数:线性变换
  • 纯虚函数必须在派生类中给出定义吗? 虚函数必须在派生类中给出定义吗?
  • Honeywell TK-PRS021 C200
  • Redis核心用法与通用命令全解析
  • Go语言中为什么map、slice、channel需要var之后还要make一下?
  • FTP Bounce Attack:原理、影响与防御
  • 如何安装和维护 Linux 系统?
  • 2025电工杯数学建模竞赛B题 城市垃圾分类运输的路径优化与调度 保姆级教程讲解|模型讲解