016、Vue-启动优化-压缩、CDN、懒加载

0、客户端CPU性能

因为Vue资源下载后需要浏览器渲染,所以客户端电脑性能本身会非常影响性能。

1、查看 report.html

Vue 查看 report.html

2、Vue 优化

2.1、启动 gzip 压缩

此方法需要借助 nginx 的功能,这种方式一般能优化一半的性能。
编译打包后,通过 nginx 访问即可。

2.1.1、安装 compression-webpack-plugin 插件

安装这个低版本就行

 npm install --save-dev compression-webpack-plugin@1.1.12
2.1.2、修改 config/index.js
# 第一步 新增
const CompressionWebpackPlugin = require('compression-webpack-plugin')

#第二步,修改 build 模块
build: {
    productionSourceMap: false,
    productionGzip: true,
    }
# 第三步 修改 module.exports 模块
module.exports = {
    //打包压缩
    configureWebpack: config => {
      if (process.env.NODE_ENV === "production") {
        return {
          // gzip压缩
          plugins: [
            new CompressionWebpackPlugin({
              algorithm: 'gzip', // 使用gzip压缩
              test: /.js$|.html$|.css$/, // 匹配文件名
              filename: '[path].gz[query]', // 压缩后的文件名(保持原文件名,后缀加.gz)
              minRatio: 1, // 压缩率小于1才会压缩
              threshold: 10240, // 对超过10k的数据压缩
              deleteOriginalAssets: false, // 是否删除未压缩的源文件,谨慎设置,如果希望提供非gzip的资源,可不设置或者设置为false(比如删除打包后的gz后还可以加载到原始资源文件)
            })
          ]
        }
      }
    },
  },
2.1.3、修改 nginx 开启 gzip

2.2、nginx开启 gzip

2.2、router 修改为懒加载

预加载模式
import HelloWorld from '@/components/HelloWorld'
 routes: [
    {
      path: '*',
      name: '默认页面',
      component: HelloWorld
    }
  ]
})
懒加载模式 import 修改为 resolve
方式1:在引用处声明

命中路由规则才会加载

//注释掉 import
//import HelloWorld from '@/components/HelloWorld'
export default new Router({
  routes: [
    {
      path: '*',
      name: '默认页面',
      component: (resolve) => require(['@/components/HelloWorld'],resolve),
    }
  ]
})

2.3、 ES 提出的import方法(推荐使用)

const HelloWorld = ()=>import('需要加载的模块地址') (不加 { } ,表示直接return)
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
const HelloWorld = () => import('@/components/HelloWorld')
export default new Router({
    routes: [
        {
            path: '/',
            name: 'HelloWorld',
            component: HelloWorld
        }
    ]
})

2.4、CDN 加速

2.4.1、直接写死的demo
2.4.1.0、CDN 网站选择

登录他们的主页进行搜索即可

推荐-一般这俩就够用了
jsdelivrhttps://www.jsdelivr.com在这里插入图片描述
bootcdnhttps://www.bootcdn.cn在这里插入图片描述
staticfilehttps://www.staticfile.org
2.4.1.1、在 index.html 中添加 js/css 引用

注意保持特定的版本[package.json],CDN 的要义就是从网络环境获取特定 js/css 资源

<head>
   <!-- built files will be auto injected -->
     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/element-theme-chalk@2.15.14/lib/index.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/muse-ui@3.0.2/dist/muse-ui.min.css">
    <link href="https://cdn.bootcss.com/material-design-icons/3.0.1/iconfont/material-icons.css" rel="stylesheet"/>

    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.2/dist/vue.min.js" rel="stylesheet" type="text/javascript"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue-router@3.2.0/dist/vue-router.min.js" rel="stylesheet" type="text/javascript" ></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.6/index.min.js" rel="stylesheet" type="text/javascript"></script>
    <script src="https://cdn.jsdelivr.net/npm/vuex@3.6.2/dist/vuex.min.js" rel="stylesheet" type="text/javascript"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios@1.6.2/dist/axios.min.js" rel="stylesheet" type="text/javascript"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/Mock.js/1.0.0/mock-min.js"></script>
  </head>
2.4.1.2、修改conf/index.js

module.exports 内增加以下内容

module.exports = {
  //CDN
  configureWebpack: {
    externals: {
      'vue': 'Vue',
      //'querystring': 'Qs',
      'vue-router': 'VueRouter',
      'element-ui': 'ELEMENT',
      //'MuseUI': 'MuseUI',
      'axios': 'axios',
      //'jquery': "jQuery",
      //"EXIF": 'EXIF',
      //'echarts': 'echarts',
      'mockjs': 'Mock'
    },
  },
 }
2.4.1.3、Vue.use(插件名)[注意CND场景下 插件名 和 import 不同]

CND 引入的话,不用 import 插件,但是需要 Vue.use(插件),需要注意的是,插件名有一个映射关系,上面 externals

//左边 是 import 方式的名字,右边是CDN方式直接 Vue.use 的名字
'vue': 'Vue',
'querystring': 'Qs',
    'vue-router': 'VueRouter',
    'element-ui': 'ELEMENT',
    'MuseUI': 'MuseUI',
    'axios': 'axios',
    'jquery': "jQuery",
    "EXIF": 'EXIF',
    'echarts': 'echarts'
  • 比较来理解:在 mian.js 中下面两种写法等效
//非CDN方式
import ElementAAA from 'element-ui'
Vue.use(ElementAAA )
//CDN方式,必须固定为Element ,其它插件也一样
Vue.use(Element )
2.4.2、 CDN 方式使用 Element
Vue 实例

现在 main.js 声明

import {
  Message
} from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css'
Vue.prototype.$message = Message;

然后在 Vue 实例中使用

this.$message.success("success")
Js 中
ELEMENT.Message.success("success");
  • 完整版
//import {Message} from 'element-ui';
//const ELEMENT = require('ELEMENT');

const showMessage = Symbol('showMessage')
class JavaboyMessage {
    [showMessage](type, options, single) {
        // if (single) {
        //     if (document.getElementsByClassName('el-message').length === 0) {
        //         Message[type](options)
        //     }
        // } else {
        //     Message[type](options)
        // }

        ELEMENT.Message[type](options);
    }
    info(options, single = true) {
        this[showMessage]('info', options, single)
    }
    warning(options, single = true) {
        this[showMessage]('warning', options, single)
    }
    error(options, single = true) {
        this[showMessage]('error', options, single)
    }
    success(options, single = true) {
        this[showMessage]('success', options, single)
    }
}
export const mymessage = new JavaboyMessage();

参考

https://blog.youkuaiyun.com/mzl87/article/details/113618488
https://blog.youkuaiyun.com/qq_34191778/article/details/126202249
https://www.jb51.net/article/255344.htm
https://developer.aliyun.com/article/1068057
https://blog.51cto.com/u_13028258/5754108

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值