【vue】通过cdn优化element-ui

因为vue+element-ui项目中,打包后体积并不大,但是element-ui可能又占用了很大一部分的内存,所以我们可以采用外部cdn资源来减少打包的内存。

1.注释掉main.js中导入的element-ui
import Vue from 'vue'
// import 'element-ui/lib/theme-chalk/index.css'
import App from './App.vue'
import './assets/css/global.css'
import router from './router'
import './assets/fonts/iconfont.css'
import axios from 'axios'
import TreeTable from 'vue-table-with-tree-grid'
import VueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css' // import styles
import 'quill/dist/quill.snow.css' // for snow theme
import 'quill/dist/quill.bubble.css' // for bubble theme
import * as echarts from "echarts"
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.prototype.$echarts = echarts
//导入富文本编辑器的样式
Vue.use(VueQuillEditor, /* { default global options } */)
//在request拦截器中展示进度条,Nprogress.start()
axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/'
axios.interceptors.request.use(config=>{
    console.log(config)
    NProgress.start()
    config.headers.Authorization = window.sessionStorage.getItem('token')
    return config
})
//在response拦截器中隐藏,NProgress.done()
//必须返回config
axios.interceptors.response.use(config => {
    NProgress.done()
    return config
})
// 配置请求的根路径
Vue.config.productionTip = false
Vue.component('tree-table',TreeTable)
Vue.prototype.$http = axios

Vue.filter('dateFormat',function (originVal){
    const dt = new Date(originVal)
    const y = dt.getFullYear()

    //如果有两位,不填充,否则在前面用0填充
    const m = (dt.getMonth()+1+'').padStart(2,'0')
    const d = (dt.getDate()+'').padStart(2,'0')
    const hh = (dt.getHours()+'').padStart(2,'0')
    const mm = (dt.getMinutes()+'').padStart(2,'0')
    const ss = (dt.getSeconds()+'').padStart(2,'0')
    return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})
// 通过render函数渲染App,然后挂载实例
new Vue({
    router,
    render: h => h(App)
}).$mount('#app')

在此处,我删除了vue.use(element-ui)以及注释了导入的element-ui及其样式表

2.在index.html的头部区域中,通过CDN加载element-ui的js和css样式

index.html路径为public/index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
      <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <link src="https://cdn.staticfile.org/element-ui/2.15.9/index.css">
      <title>shop</title>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but shop doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

在使用时,注意版本即可,想查看版本可以cmd打开vue ui,打开项目,然后找到运行依赖,查看版本号后,直接在路径中替换成自己的的版本号即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

渣渣高不会写Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值