路由模式和打包优化

1. 路由模式-将路由改成history模式

  • hash模式带#,#后面的地址变化不会引起页面的刷新
  • history没有#,地址变化会引起页面刷新,更符合页面地址的规范(开发环境不刷新-webpack配置)
  • 将路由模式修改成history模式-代码位置(src/router/index.js)
javascript 复制代码
const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes // 默认引入静态路由
})

2. 打包分析-分析

  • 打包分析代码
javascript 复制代码
$ npm run preview -- --report
  • 去除main.js中对于mock.js的引用

3. CDN加速

将几个比较大的多在打包时排除,这样可以缩小整体打包的大小,保证js的加载速度,排除的包采用cdn的方式用外链去引入,cdn本名为分发服务器,意为更近的访问区间更快的访问速度将所需要的文件返回给客户端

  • webpack排除打包-代码位置(vue.config.js)
javascript 复制代码
 configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    // 配置需要排出的包
    externals: {
      'vue': 'Vue',
      'element-ui': 'ELEMENT',
      'cos-js-sdk-v5': 'COS'
    }
  },
  • 在html中采用外链引入排除的文件-代码位置(public/index.html)
javascript 复制代码
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title><%= webpackConfig.name %></title>
    <link href="https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.13/theme-chalk/index.min.css" rel="stylesheet">
  </head>
  <body>
    <noscript>
      <strong>We're sorry but <%= webpackConfig.name %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
    <script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.13/index.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/cos-js-sdk-v5/dist/cos-js-sdk-v5.min.js" ></script>

  </body>
</html>

windows配置nginx

相关推荐
郭少1 分钟前
🔥 我封装了一个会“思考”的指令!Element-Plus Tooltip 自动检测文本溢出,优雅展示
前端·vue.js·性能优化
郭少5 分钟前
🔥 放弃 vw!我在官网大屏适配中踩了天坑,用 postcss-px-to-viewport-8-plugin 实现了 Rem 终极方案
vue.js·性能优化·nuxt.js
咸虾米6 分钟前
微信小程序通过uni.chooseLocation打开地图选择位置,相关设置及可能出现的问题
vue.js·微信小程序
鹏多多16 分钟前
深入解析vue的transition过渡动画使用和优化
前端·javascript·vue.js
前端小巷子1 小时前
Vue3 响应式革命
前端·vue.js·面试
杨荧2 小时前
基于Python的反诈知识科普平台 Python+Django+Vue.js
大数据·前端·vue.js·python·数据分析
zhoxier8 小时前
elementui el-select 获取value和label 以及 对象的方法
javascript·vue.js·elementui
四岁半儿12 小时前
vue,H5车牌弹框定制键盘包括新能源车牌
前端·vue.js
HANK16 小时前
KLineChart 绘制教程
前端·vue.js
Juchecar17 小时前
Naive UI 学习指南 - Vue3 初学者完全教程
前端·vue.js