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

Vue加载速度优化,verder.js和element.js加载速度慢解决方法

1. 使用CDN

这里把常用的vue、vuex、elementui、echarts、axios都引入成cdn的方式

1、在index.html引入CDN

找到public/index.html在上方引入下边的cdn。

[!NOTE]

引入script的时候,一定要把vue.js放到最上边,最先引入,不然后边的js加载会有问题

  <!-- 引入样式 --><link href="https://cdn.bootcss.com/element-ui/2.8.2/theme-chalk/index.css" rel="stylesheet"><link href="https://cdn.bootcss.com/element-ui/2.15.14/theme-chalk/index.css" rel="stylesheet"><script src="https://cdn.bootcss.com/vue/2.7.14/vue.min.js"></script><script src="https://cdn.bootcss.com/element-ui/2.15.14/index.js"></script><script src="https://cdn.bootcss.com/vue-router/3.6.5/vue-router.min.js"></script><script src="https://cdn.bootcss.com/vuex/3.6.2/vuex.min.js"></script><script src="https://cdn.bootcss.com/axios/1.11.0/axios.min.js"></script><script src="https://cdn.bootcss.com/echarts/6.0.0/echarts.min.js"></script><script src="https://cdn.bootcdn.net/ajax/libs/vue-echarts/6.7.3/index.esm.min.js"></script>

2、批量注释掉引用

main.js项目主js或者ts中去除对element 、vue、vuex、echarts、axios的引用

全局搜索

// import Vue from 'vue'
// import Vuex from 'vuex'
// import ElementUI from 'element-ui'
// import 'echarts'
// import request from 'axios'
// import { MessageBox } from 'element-ui'
// import { Loading, Message, MessageBox, Notification } from 'element-ui'

把这些直接引用的地方都注释掉

3、main.js(指定文件排查)

在mian.js中注释掉 element 、vue、vuex、echarts、axios的引用

其中**Vue.use(Elment)**要注释掉。不然启动会报错

4、router/index.js(指定文件排查)

注释掉

// import Vue from 'vue'
// import VueRouter from 'vue-router'

保留

Vue.use(VueRouter)

[!NOTE]

这里要注意一下,Vue.use(VueRouter)中的VueRouter不能改为其他字段,否则会报错

5、store/index.js(指定文件排查)

注释掉

// import Vue from 'vue'
// import Vuex from 'vuex'

保留

Vue.use(Vuex)

6、webpack出去除依赖

在webpack的配置文件中去除对vue、vuex、axios、echarts、element等的依赖。

查看主目录的vue.config.js或者webpack.config.js之类的打包配置文件

也可以搜索module.exports = {此文件中就可以加上配置,去除以来

module.exports = {// 引入外部库, 无需webpack打包处理externals: {'vue' : 'Vue','vue-router':'VueRouter','vuex':'Vuex','axios':'axios','element-ui':'ElementUI','mockjs': 'Mock','echarts': 'echarts','ueditor': 'UE'}
}

2. 路由懒加载

router/index.js

路由使用懒加载模式

// import Vue from 'vue'
// import VueRouter from 'vue-router'
import Layout from '@/layouts'
import { publicPath, routerMode } from '@/config'Vue.use(VueRouter)
export const constantRoutes = [{path: '/login',component: () => import('@/views/login/index'),hidden: true,},{path: '/register',component: () => import('@/views/register/index'),hidden: true,},{path: '/401',name: '401',component: () => import('@/views/401'),hidden: true,},{path: '/404',name: '404',component: () => import('@/views/404'),hidden: true,},
]
// 路由懒加载
const device = ()=> import("@/views/device/index")
const deviceVersion = ()=> import("@/views/deviceVersion/index")
const mqttUser = ()=> import("@/views/mqttUser/index")
const role = ()=> import("@/views/personnelManagement/roleManagement/index")
const user = ()=> import("@/views/personnelManagement/userManagement/index")
export const asyncRoutes = [{path: '/',component: Layout,redirect: '/index',children: [{path: 'index',name: 'Index',component: () => import('@/views/index/index'),meta: {title: '首页',icon: 'home',affix: true,},},],},{path: "/device",component: Layout,redirect: "noRedirect",children: [{path: "index",name: "Index",component: device,meta: {title: "设备管理",icon: "vector-square",permissions: ["admin"],},},],},{path: "/deviceVersion",component: Layout,redirect: "noRedirect",children: [{path: "index",name: "Index",component: deviceVersion,meta: {title: "设备版本",icon: "cloud-upload-alt",permissions: ["admin"],},},],},{path: "/mqttUser",component: Layout,redirect: "noRedirect",children: [{path: "index",name: "Index",component: mqttUser,meta: {title: "设备接入",icon: "cube",permissions: ["admin"],},},],},{path: "/role",component: Layout,redirect: "noRedirect",children: [{path: "index",name: "Index",component: role,meta: {title: "角色管理",icon: "diagnoses",permissions: ["admin"],},},],},{path: "/user",component: Layout,redirect: "noRedirect",children: [{path: "index",name: "Index",component: user,meta: {title: "用户管理",icon: "user-friends",permissions: ["admin"],},},],},{path: '*',redirect: '/404',hidden: true,},
]const router = new VueRouter({base: publicPath,mode: routerMode,scrollBehavior: () => ({y: 0,}),routes: constantRoutes,
})export function resetRouter() {location.reload()
}export default router

3. Nginx开启Gzip压缩

httpserver 模块中添加配置

server {# 其他配置...gzip on; # 开启gzip压缩gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/png image/gif image/jpeg; # 设置需要压缩的文件类型gzip_comp_level 6; # 设置gzip的压缩级别,1-9,9最压缩,但最耗时gzip_buffers 16 8k; # 设置系统获取几个单server存储gzip的压缩结果数据流。gzip_http_version 1.1; # 设置识别HTTP协议版本,默认1.1gzip_vary on; # 此指令可以让前端的缓存服务器缓存在不同的压缩方式上。# gzip_proxied any; # 对于后端服务器返回的数据进行压缩,默认不对数据进行压缩。
}

重启nginx使配置生效

/usr/local/nginx/sbin/nginx -s reload
http://www.xdnf.cn/news/19353.html

相关文章:

  • 防火墙技术(二):安全区域
  • C#调用c++ dll读取2进制文件时而正常,时而异常
  • 语义分割目前还是研究热点吗?
  • 如何快速了解项目管理基础
  • 【具身智能】【机械臂】机械臂轨迹规划项目以及资料汇总【持续更新】
  • 【物联网】MQTT / Broker / Topic 是什么?
  • windows 谷歌浏览器把英文改成中文
  • 【路由器】TP Link 路由器为何无法进入管理后台
  • 关于铭飞平台企业官网模板使用中常到的问题、企业官网的百度认证以及IDEA编辑启动器的快捷方法/Apipost本地和云端没法同步的问题解决
  • 【软考架构】SOA与微服务解疑
  • React Hooks深度解析与最佳实践:提升函数组件能力的终极指南
  • Unity笔记(八)——资源动态加载、场景异步加载
  • 迷你电脑用到什么型号的RJ45网口
  • 揭秘表格推理的“思维革命”:RoT模型介绍
  • seafile-setup-troubleshooting_# Seafile 安装与问题解决记录 # Seafile/Seahub 启动问题记录文档
  • linux基础——UDP、TCP
  • JavaScript之性能优化
  • 深入理解C++中的移动赋值与拷贝赋值函数——兼论移动构造函数及其实际应用场景
  • STM32手动移植FreeRTOS
  • 【学Python自动化】 1. Python 安装与配置完全指南 (Windows)
  • 从“互联网+”到“人工智能+”:云计算生态演进揭示AI应用破局之道
  • springboot 实现不同接口指定上传文件大小
  • 腾讯云centos7.6的运维笔记——从yum的安装与更新源开始
  • 小狼毫输入法中让数字键盘上的数字键不再选择候选词而是与原始输入一起直接上屏
  • 抖音热榜展示页面
  • Android 权限申请现代化指南
  • MySQL 在 CentOS 上的安装与配置文件路径详解
  • 2025-08-18面试题(nginx,mysql,zabbix为主)
  • LeetCode 2540.最小公共值
  • 1.7 Rendering模块