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

相关文章

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

qt5cored.dll报错怎么解决? 电脑qt5cored.dll文件丢失修复技巧

《qt5cored.dll报错怎么解决?电脑qt5cored.dll文件丢失修复技巧》在进行软件安装或运行程序时,有时会遇到由于找不到qt5core.dll,无法继续执行代码,这个问题可能是由于该文... 遇到qt5cored.dll文件错误时,可能会导致基于 Qt 开发的应用程序无法正常运行或启动。这种错

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

MySQL 设置AUTO_INCREMENT 无效的问题解决

《MySQL设置AUTO_INCREMENT无效的问题解决》本文主要介绍了MySQL设置AUTO_INCREMENT无效的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录快速设置mysql的auto_increment参数一、修改 AUTO_INCREMENT 的值。