vue项目中不打包库文件

目的:将所有的库文件提出来,项目打包时不打包这些库文件。
以一个简易 \color{red}{vue}项目举例:

1.将库文件放入 static文件夹中

├── src
├── static                
     ├── img 
     ├── css 
     │    ├── fonts  
     │    │     ├── ionicons.ttf 
     │    │     └── ionicons.woff  
     │    ├── base.css  
     │    └── iview.css  
     └── js  
          ├── axios.min.js  
          ├── es6-promise.auto.js 
          ├── iview.js 
          ├── vue.min.js 
          ├── vue-router.min.js 
          └── vuex.min.js 

2.入口文件index.html文件中引入

  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/iview.css">
  <script src="static/js/vue.min.js"></script>
  <script src="static/js/iview.js"></script>
  <script src="static/js/vuex.min.js"></script>
  <script src="static/js/vue-router.min.js"></script>
  <script src="static/js/axios.min.js"></script>
  <script src="static/js/es6-promise.auto.min.js"></script>  <!--使不支持promise(例如IE)的浏览器支持promise--> 

3.在项目中直接使用,例如src/main.js配置(这里将axios,router,vuex都写在了一起为了方便说明)。

// import Vue from 'vue'  不需要
//直接使用 Vue  
import App from './App'
Vue.config.productionTip = false

//import axios from 'axios'  不需要
//直接使用 axios
Vue.prototype.$axios = axios
axios.defaults.timeout = 5000                     
axios.defaults.baseURL = '/api'                                
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
axios.interceptors.request.use((config) => {      
  //......(发送请求前,对提交数据做处理)
  return config
},(error) =>{
  return Promise.reject(error)
})
axios.interceptors.response.use((res) =>{
  //......(对响应数据做处理)
  return res;
}, (error) => {
  return Promise.reject(error)
})

//import Router from 'vue-router'  不需要
//Vue.use(Router)  不需要
//直接使用 VueRouter
const router = new VueRouter({
  mode: 'history',
  routes:[
    {
      path: '/',
      name: 'home',
      component: () => import('@/components/index')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/components/login/index')
    }
  ] 
})

//import Vuex from 'vuex'    不需要
//Vue.use(Vuex)   不需要
//直接使用 Vuex
const store = Vuex.Store({
  state:{  
    isLoding:false,                   
  },
  getters:{    
    isLoding(state){  return state.isLoding } ,         
  },
  mutations:{
    loadingSet(state,bool){ state.isLoding = !!bool ? true : false },
  },
  actions:{
    loading(context,bool){ context.commit('loadingSet',bool) },
  }
})

new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

4.然后 npm run build 打包时就不会把static中的文件压缩编译到一起。

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ## 框架和库的区别?> 框架(framework):一套完整的软件设计架构和**解决方案**。> > 库(lib...
    Rui_bdad阅读 3,258评论 1赞 4
  • 来源:http://www.cnblogs.com/opendigg/p/6513510.html UI组件 el...
    YU_XI阅读 2,986评论 0赞 25
  • vue-cli搭建项目 确保安装了node与npm 再目标文件夹下打开终端 执行cnpm i vue-cli -g...
    Akiko_秋子阅读 3,448评论 1赞 22
  • “有时候会觉得这个世界真不公平,为什么那么多人随便的活活就那么精彩,而我拼了命的生活,却只是一种生存?” “为什么...
    苏阿花_阅读 552评论 2赞 0
  • 半夜盛开, 期待, 一场淋漓尽致的大雨, 冲刷它的娇嫩, 让它, 茁壮成长起来, 不再, 柔弱任风吹晒, 不会, ...

友情链接更多精彩内容