vue2 项目中使用CDN

1.在webpack的配置文件中添加configureWebpack属性是一个对象,在这个对象中添加externals属性(也是一个对象),在这个对象中添加打包是要排除的包:
 vue.config.js

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    open: true,
    host: '127.0.0.1',
    port: 80
  },
  publicPath: process.env.NODE_ENV === 'production' ? './' : '/',
  configureWebpack: {
    externals: process.env.NODE_ENV === 'production'
      ? {
          axios: 'axios',
          dayjs: 'dayjs',
          echarts: 'echarts',
          'element-ui': 'ELEMENT',
          vue: 'Vue',
          'vue-quill-editor': 'VueQuillEditor',
          'vue-router': 'VueRouter',
          vuex: 'Vuex',
          'vuex-persistedstate': 'createPersistedState'
        }
      : {}
  }

})
2.在再public.html 引入对应的包
包可以再https://unpkg.com/网站中查找

** 使用: unpkg.com/:package@:version/:file 如找 vue@2.6.14 在浏览器中数 unpkg.com/vue@2.6.14 后回车 再 复制浏览器中的网址**

public.html 
<!DOCTYPE html>
<html lang="">
<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">
  <title>
    <%= htmlWebpackPlugin.options.title %>
  </title>
  <% if(process.env.NODE_ENV === 'production') { %>
  <link rel="stylesheet" href="https://unpkg.com/quill@1.3.7/dist/quill.core.css">
  <link rel="stylesheet" href="https://unpkg.com/quill@1.3.7/dist/quill.snow.css">
  <link rel="stylesheet" href="https://unpkg.com/quill@1.3.7/dist/quill.bubble.css">
  <link rel="stylesheet" href="https://unpkg.com/element-ui@2.15.13/lib/theme-chalk/index.css">
  <% } %>
</head>
<body>
  <noscript>
    <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled.
        Please enable it to continue.</strong>
  </noscript>
  <div id="app"></div>
  <!-- built files will be auto injected -->
  <% if(process.env.NODE_ENV === 'production') { %>
  <script src="https://unpkg.com/axios@1.3.3/dist/axios.min.js"></script>
  <script src="https://unpkg.com/dayjs@1.11.7/dayjs.min.js"></script>
  <script src="https://unpkg.com/echarts@5.4.1/dist/echarts.js"></script>
  <script src="https://unpkg.com/vue@2.6.14/dist/vue.js"></script>
  <script src="https://unpkg.com/vue-quill-editor@3.0.6/dist/vue-quill-editor.js"></script>
  <script src="https://unpkg.com/element-ui@2.15.13/lib/index.js"></script>
  <script src="https://unpkg.com/vue-router@3.5.1/dist/vue-router.js"></script>
  <script src="https://unpkg.com/vuex@3.6.2/dist/vuex.js"></script>
  <script src="https://unpkg.com/vuex-persistedstate@3.2.1/dist/vuex-persistedstate.umd.js"></script>
  <% } %>
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值