模块化路由解决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

相关文章

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

504 Gateway Timeout网关超时的根源及完美解决方法

《504GatewayTimeout网关超时的根源及完美解决方法》在日常开发和运维过程中,504GatewayTimeout错误是常见的网络问题之一,尤其是在使用反向代理(如Nginx)或... 目录引言为什么会出现 504 错误?1. 探索 504 Gateway Timeout 错误的根源 1.1 后端