模块化路由解决vue项目中路由文件越来越大的问题

本文主要是介绍模块化路由解决vue项目中路由文件越来越大的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

随着项目的不断扩展原始路由文件越来越大,如下

//index.js 文件内容import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/waf_flow_alarm/frame/Home'
import Login from '@/components/waf_flow_alarm/page/system/login.vue'
import ChangePwd from '@/components/waf_flow_alarm/page/system/changepwd.vue'Vue.use(Router)const originalPush = Router.prototype.pushRouter.prototype.push = function push(location) {return originalPush.call(this, location).catch(err => err)
}export default new Router({routes: [{path: '/login',name: 'login',component: Login,},{path: '/changepwd/:username',name: 'changepwd',component: ChangePwd,},{path: '/',name: 'Home',component: Home,redirect:'/front',children:[{path: '/front',name: 'front',component: () => import("@/components/waf_flow_alarm/page/system/front.vue")},{path: '/empty',name: 'empty',component: () => import("@/components/waf_flow_alarm/page/system/empty.vue")},{path: '/wafStatusWiki',name: 'wafStatusWiki',component: () => import("@/components/waf_flow_alarm/page/wiki/wafStatusWiki.vue")},{path: '/defenseIpQuery',name: 'defenseIpQuery',component: () => import("@/components/waf_flow_alarm/page/monitor_query/defenseIpQuery.vue")},{path: '/defenseDomainQuery',name: 'defenseDomainQuery',component: () => import("@/components/waf_flow_alarm/page/monitor_query/defenseDomainQuery.vue")},{path: '/defenseIpHistoryQuery',name: 'defenseIpHistoryQuery',component: () => import("@/components/waf_flow_alarm/page/monitor_query/defenseIpHistoryQuery.vue")},{path: '/defenseDomainHistoryQuery',name: 'defenseDomainHistoryQuery',component: () => import("@/components/waf_flow_alarm/page/monitor_query/defenseDomainHistoryQuery.vue")},{path: '/currentBlock',name: 'currentBlock',component: () => import("@/components/waf_flow_alarm/page/monitor_query/currentBlock.vue")},{path: '/historyBlock',name: 'historyBlock',component: () => import("@/components/waf_flow_alarm/page/monitor_query/historyBlock.vue")},{path: '/ccBlock',name: 'ccBlock',component: () => import("@/components/waf_flow_alarm/page/monitor_query/ccBlock.vue")},{path: '/proActiveUserAgent',name: 'proActiveUserAgent',component: () => import("@/components/waf_flow_alarm/page/active_defense/proActiveUserAgent.vue")},{path: '/proActiveUserAgentQwmh',name: 'proActiveUserAgentQwmh',component: () => import("@/components/waf_flow_alarm/page/active_defense/proActiveUserAgentQwmh.vue")},{path: '/proActiveBlockIpByNginx',name: 'proActiveBlockIpByNginx',component: () => import("@/components/waf_flow_alarm/page/active_defense/proActiveBlockIpByNginx.vue")},{path: '/proActiveBlockDomainByNginx',name: 'proActiveBlockDomainByNginx',component: () => import("@/components/waf_flow_alarm/page/active_defense/proActiveBlockDomainByNginx.vue")},{path: '/proActiveBlockRefererByNginx',name: 'proActiveBlockRefererByNginx',component: () => import("@/components/waf_flow_alarm/page/active_defense/proActiveBlockRefererByNginx.vue")},{path: '/proActiveUri',name: 'proActiveUri',component: () => import("@/components/waf_flow_alarm/page/active_defense/proActiveUri.vue")},{path: '/proManmachineIp',name: 'proManmachineIp',component: () => import("@/components/waf_flow_alarm/page/active_defense/proManmachineIp.vue")},{path: '/proManmachineDomain',name: 'proManmachineDomain',component: () => import("@/components/waf_flow_alarm/page/active_defense/proManmachineDomain.vue")},{path: '/proQuestionIp',name: 'proQuestionIp',component: () => import("@/components/waf_flow_alarm/page/active_defense/proQuestionIp.vue")},{path: '/dbinfosearch',name: 'dbinfosearch',component: () => import("@/components/waf_flow_alarm/page/conf_manage/dbinfosearch.vue")},{path: '/ipinfosearch',name: 'ipinfosearch',component: () => import("@/components/waf_flow_alarm/page/conf_manage/ipinfosearch.vue")},{path: '/sqlproxyhostsearch',name: 'sqlproxyhostsearch',component: () => import("@/components/waf_flow_alarm/page/conf_manage/sqlproxyhostsearch.vue")},{path: '/alarmPoolCfg',name: 'alarmPoolCfg',component: () => import("@/components/waf_flow_alarm/page/conf_manage/alarmPoolCfg.vue")},{path: '/alarmPoolDetail',name: 'alarmPoolDetail',component: () => import("@/components/waf_flow_alarm/page/conf_manage/alarmPoolDetail.vue")},{path: '/alarmSingleCfg',name: 'alarmSingleCfg',component: () => import("@/components/waf_flow_alarm/page/conf_manage/alarmSingleCfg.vue")},{path: '/alarmRawdata',name: 'alarmRawdata',component: () => import("@/components/waf_flow_alarm/page/conf_manage/alarmRawdata.vue")},{path: '/thirdpartyNginxBlockIp',name: 'thirdpartyNginxBlockIp',component: () => import("@/components/waf_flow_alarm/page/conf_manage/thirdpartyNginxBlockIp.vue")},{path: '/alarmWhitelistIp',name: 'alarmWhitelistIp',component: () => import("@/components/waf_flow_alarm/page/conf_manage/alarmWhitelistIp.vue")},{path: '/alarmWhitelistDomain',name: 'alarmWhitelistDomain',component: () => import("@/components/waf_flow_alarm/page/conf_manage/alarmWhitelistDomain.vue")},{path: '/roleManage',name: 'roleManage',component: () => import("@/components/waf_flow_alarm/page/system/roleManage.vue")},{path: '/userManage',name: 'userManage',component: () => import("@/components/waf_flow_alarm/page/system/userManage.vue")},{path: '/resourceManage',name: 'resourceManage',component: () => import("@/components/waf_flow_alarm/page/system/resourceManage.vue")},{path: '/actionLog',name: 'actionlog',component: () => import("@/components/waf_flow_alarm/page/system/actionLog.vue")},{path: '/dictManage',name: 'dictManage',component: () => import("@/components/waf_flow_alarm/page/system/dictManage.vue")}]}]
})

路由配置越来越大,导致代码的可读性和维护性变差。为了解决这个问题,你可以采取以下几个优化措施:

1. 模块化路由配置

将路由按模块拆分到多个文件中,并在主路由文件中进行导入。这不仅能减少单个文件的长度,还能使得不同模块的路由配置更加清晰。

2. 动态路由加载

通过 require.context 动态加载路由配置,减少手动导入的代码量。

3. 懒加载组件

对所有的组件使用懒加载,这样不仅可以优化路由文件,还能提升应用性能。

以下是具体的实现步骤:

Step 1: 创建路由模块

src/router/modules/ 文件夹下创建多个路由模块文件,每个文件包含特定模块的路由配置。例如:

  • src/router/modules/system.js
  • src/router/modules/monitor_query.js
  • src/router/modules/active_defense.js
  • src/router/modules/conf_manage.js
// src/router/modules/system.js
export default [{path: '/roleManage',name: 'roleManage',component: () => import("@/components/waf_flow_alarm/page/system/roleManage.vue")},{path: '/userManage',name: 'userManage',component: () => import("@/components/waf_flow_alarm/page/system/userManage.vue")},// 其他 system 模块路由
];

其他模块类似配置。

Step 2: 在主路由文件中导入这些模块

在主路由文件中导入这些模块的路由配置,并合并它们:

// src/router/index.js
import Vue from 'vue';
import Router from 'vue-router';import systemRoutes from './modules/system';
import monitorQueryRoutes from './modules/monitor_query';
import activeDefenseRoutes from './modules/active_defense';
import confManageRoutes from './modules/conf_manage';Vue.use(Router);const routes = [{path: '/login',name: 'login',component: () => import('@/components/Login.vue')},{path: '/changepwd/:username',name: 'changepwd',component: () => import('@/components/ChangePwd.vue')},{path: '/',name: 'Home',component: () => import('@/components/Home.vue'),redirect: '/front',children: [{path: '/front',name: 'front',component: () => import('@/components/waf_flow_alarm/page/system/front.vue')},// 使用模块化路由...systemRoutes,...monitorQueryRoutes,...activeDefenseRoutes,...confManageRoutes,]}
];export default new Router({routes
});
Step 3: 懒加载所有组件

确保在所有路由中,组件的导入都是通过懒加载的方式:

component: () => import('@/components/YourComponent.vue')

总结

通过上述方法,你可以将路由配置拆分成多个模块,提升代码的可维护性,同时通过懒加载优化性能。


a. 继续为其他项目模块编写模块化路由文件。

b. 添加路由守卫或动态路由处理逻辑以进一步优化代码。

这篇关于模块化路由解决vue项目中路由文件越来越大的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1094584

相关文章

vite搭建vue3项目的搭建步骤

《vite搭建vue3项目的搭建步骤》本文主要介绍了vite搭建vue3项目的搭建步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1.确保Nodejs环境2.使用vite-cli工具3.进入项目安装依赖1.确保Nodejs环境

Nginx搭建前端本地预览环境的完整步骤教学

《Nginx搭建前端本地预览环境的完整步骤教学》这篇文章主要为大家详细介绍了Nginx搭建前端本地预览环境的完整步骤教学,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录项目目录结构核心配置文件:nginx.conf脚本化操作:nginx.shnpm 脚本集成总结:对前端的意义很多

IDEA和GIT关于文件中LF和CRLF问题及解决

《IDEA和GIT关于文件中LF和CRLF问题及解决》文章总结:因IDEA默认使用CRLF换行符导致Shell脚本在Linux运行报错,需在编辑器和Git中统一为LF,通过调整Git的core.aut... 目录问题描述问题思考解决过程总结问题描述项目软件安装shell脚本上git仓库管理,但拉取后,上l

前端缓存策略的自解方案全解析

《前端缓存策略的自解方案全解析》缓存从来都是前端的一个痛点,很多前端搞不清楚缓存到底是何物,:本文主要介绍前端缓存的自解方案,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、为什么“清缓存”成了技术圈的梗二、先给缓存“把个脉”:浏览器到底缓存了谁?三、设计思路:把“发版”做成“自愈”四、代码

通过React实现页面的无限滚动效果

《通过React实现页面的无限滚动效果》今天我们来聊聊无限滚动这个现代Web开发中不可或缺的技术,无论你是刷微博、逛知乎还是看脚本,无限滚动都已经渗透到我们日常的浏览体验中,那么,如何优雅地实现它呢?... 目录1. 早期的解决方案2. 交叉观察者:IntersectionObserver2.1 Inter

解决docker目录内存不足扩容处理方案

《解决docker目录内存不足扩容处理方案》文章介绍了Docker存储目录迁移方法:因系统盘空间不足,需将Docker数据迁移到更大磁盘(如/home/docker),通过修改daemon.json配... 目录1、查看服务器所有磁盘的使用情况2、查看docker镜像和容器存储目录的空间大小3、停止dock

Spring Gateway动态路由实现方案

《SpringGateway动态路由实现方案》本文主要介绍了SpringGateway动态路由实现方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录前沿何为路由RouteDefinitionRouteLocator工作流程动态路由实现尾巴前沿S

Vue3视频播放组件 vue3-video-play使用方式

《Vue3视频播放组件vue3-video-play使用方式》vue3-video-play是Vue3的视频播放组件,基于原生video标签开发,支持MP4和HLS流,提供全局/局部引入方式,可监听... 目录一、安装二、全局引入三、局部引入四、基本使用五、事件监听六、播放 HLS 流七、更多功能总结在 v

idea npm install很慢问题及解决(nodejs)

《ideanpminstall很慢问题及解决(nodejs)》npm安装速度慢可通过配置国内镜像源(如淘宝)、清理缓存及切换工具解决,建议设置全局镜像(npmconfigsetregistryht... 目录idea npm install很慢(nodejs)配置国内镜像源清理缓存总结idea npm in

idea+spring boot创建项目的搭建全过程

《idea+springboot创建项目的搭建全过程》SpringBoot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目,:本文主要介绍idea+springb... 目录一.idea四种搭建方式1.Javaidea命名规范2JavaWebTomcat的安装一.明确tomcat