vue前端性能优化之cdn的使用

优化原因

项目中首页加载过慢, 原因是js,css 静态资源第一次加载的时间长

vue.config.js

'use strict'
...

...
const cdn = {
  js_cdn: [
    'https://cdn.jsdelivr.net/npm/moment@2.27.0/moment.min.js',
    'https://unpkg.com/vue@2.6.10/dist/vue.js',
    'https://unpkg.com/vue-i18n@8.18.2/dist/vue-i18n.js',
    'https://cdn.staticfile.org/vue-router/3.0.6/vue-router.min.js',
    'https://cdn.staticfile.org/axios/0.18.1/axios.min.js',
    'https://cdn.bootcdn.net/ajax/libs/echarts/4.9.0-rc.1/echarts.js',
    'https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.js',
    'https://cdn.staticfile.org/vuex/3.1.0/vuex.min.js',
    'https://unpkg.com/element-ui@2.13.0/lib/index.js',
    'https://cdn.jsdelivr.net/npm/echarts-liquidfill@2.0.6/dist/echarts-liquidfill.min.js',
    'https://unpkg.com/element-ui@2.15.1/lib/umd/locale/zh-CN.js',
    'https://unpkg.com/element-ui@2.15.1/lib/umd/locale/zh-TW.js',
    'https://unpkg.com/element-ui@2.15.1/lib/umd/locale/en.js'
  ],
  css_cdn: [
    'https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.css',
    'https://cdn.jsdelivr.net/npm/element-ui@2.13.0/lib/theme-chalk/index.css'
  ],
  lang: true
}
// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
// You can change the port by the following methods:
// port = 9528 npm run dev OR npm run dev --port = 9528
// const port = process.env.port || process.env.npm_config_port || 8080 // dev port

// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
  ..
  css: {
    ...
  },
  configureWebpack: {
   ....
  },
  chainWebpack(config) {
    ...
   ....

    config
      // https://webpack.js.org/configuration/devtool/#development
      .when(process.env.NODE_ENV === 'development',
        config => {
          config.devtool('cheap-source-map')
          config.entry('app').clear().add('./src/main.js')
        }
      )

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config.entry('app').clear().add('./src/prod.js')
          config.set('externals', {
            vue: 'Vue',
            vuex: 'Vuex',
            'vue-router': 'VueRouter', // 必须是VueRouter,不然打包后运行会报错
            axios: 'axios',
            echarts: 'echarts',
            nprogress: 'NProgress',
            moment: 'moment',
            'vue-i18n': 'VueI18n'
          })
          config.plugin('html')
            .tap(args => {
              args[0].cdn = cdn
              return args
            })
            ......
        }
      )
  }
}

index.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">
    <meta http-equiv="pragram" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate">
    <% for(let i in htmlWebpackPlugin.options.cdn && htmlWebpackPlugin.options.cdn.css_cdn) { %>
      <link href="<%= htmlWebpackPlugin.options.cdn.css_cdn[i] %>" rel="preload" as="style">
      <link href="<%= htmlWebpackPlugin.options.cdn.css_cdn[i] %>" rel="stylesheet">
    <% } %>
    <% for(let i in htmlWebpackPlugin.options.cdn && htmlWebpackPlugin.options.cdn.js_cdn) { %>
      <link href="<%= htmlWebpackPlugin.options.cdn.js_cdn[i] %>" rel="preload" as="script">
    <% } %>
    <link rel="icon" href="<%= BASE_URL %>newlogo.svg">
    <script src="<%= BASE_URL %>config.js"></script>
    <title><%= webpackConfig.name %></title>
  </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 -->
    <% for (var i in htmlWebpackPlugin.options.cdn && htmlWebpackPlugin.options.cdn.js_cdn) { %>
      <script src="<%= htmlWebpackPlugin.options.cdn.js_cdn[i] %>"></script>
    <% } %>
    
  </body>

国际化,关于elementUI

经历了n次的失败,辛酸路程也不想在这里 谈,直接上代码

i18n/index.js

这里为了开发和生产模式都可以正常使用element的国际化,所以设置ELEMENT常量,
开发环境中:
-------------- 直接获取node_modules中语言包js的文件位置,导入(这里使用require动态导入),记得.default[都是被坑出来的]。
生产环境中:
--------------由于elementui使用cdn,怎会自动导入全局window.ELEMENT,这里切勿直接写ELEMENT,这样获取到的不是你想要的。

import Vue from 'vue'
import VueI18n from 'vue-i18n'
import Cookies from 'js-cookie'

import en from './config/en'
import zhmo from './config/zh-mo'
import zh from './config/zh'
Vue.use(VueI18n)
const ELEMENT = window.ELEMENT || process.env.NODE_ENV === 'development' && {
  lang: {
    zhCN: require('element-ui/lib/locale/lang/zh-CN').default,
    zhTW: require('element-ui/lib/locale/lang/zh-TW').default,
    en: require('element-ui/lib/locale/lang/en').default
  }
}

// 创建vue-i18n实例
const i18n = new VueI18n({
  // 设置默认语言
  locale: Cookies.get('language') || 'zh',
  // 添加多语言
  messages: {
    zh: {
      ...zh,
      ...ELEMENT.lang.zhCN
    },
    zhmo: {
      ...zhmo,
      ...ELEMENT.lang.zhTW
    },
    en: {
      ...en,
      ...ELEMENT.lang.en
    }
  },
  silentTranslationWarn: true
})

export default i18n

不是vue文件中,使用ElementUI的组件

比如你想在定义axios中的文件中,使用Message去给出一些提示,那么你就需要根据是否为开发环境,而对获取element的这个常量就该选取不同的位置

const ELEMENT = window.ELEMENT || process.env.NODE_ENV === 'development' && require('element-ui')

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 前端性能优化可以从以下几个方面入手: 1. 代码优化:使用合适的数据绑定方式,避免不必要的计算和渲染。合理使用计算属性、watcher 和 v-if/v-show 等指令,避免频繁的重新渲染。同时,注意避免在模板中使用复杂的表达式和函数调用。 2. 组件拆分和懒加载:将大型组件拆分成更小的组件,按需加载。这样可以降低首次加载时的资源压力,提高页面初始化速度。使用 Vue 的异步组件和路由懒加载功能,可以在需要时再去加载组件。 3. 图片优化:使用合适的图片格式和压缩工具来减小图片大小,提高加载速度。可以使用工具将图片转为 WebP 格式,并通过 CDN 加速图片加载。 4. 路由优化:使用懒加载和异步组件,减少首次加载时需要加载的资源。合理使用路由懒加载,按需加载所需的组件。 5. 数据请求优化:减少不必要的接口请求,合并接口请求,避免频繁的网络请求。合理使用缓存和本地存储,减少对服务器的依赖。 6. 代码分割和打包优化:使用工具将代码按需分割成多个文件,利用浏览器的并行加载能力,加快页面加载速度。对打包后的资源进行压缩和混淆,减小文件大小。 7. 使用虚拟列表和无限滚动:对于大量数据的展示,可以使用虚拟列表或者无限滚动技术,只渲染可见区域的数据,提高列表性能。 8. 优化重绘和回流:避免频繁的 DOM 操作和样式改变,合理使用 CSS transform 和 opacity 等属性来减少重绘和回流。将频繁改变的元素设置为 fixed 或 absolute 定位,可以避免重新布局。 以上是一些常见的 Vue 前端性能优化的方法,根据具体项目的需求和实际情况,可以选择适合的优化方案来提高页面性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值