Vue-Cli3.0常用配置 (六)

一.使用vue-cli3.0创建vue项目

vue create <project-name>
1

可以通过vue ui创建项目/管理项目依赖

vue ui
1

二.配置vue-config.js

let path = require('path');
let skeletionWebpackPlugin = require('vue-skeleton-webpack-plugin');


module.exports = {
    publicPath:process.env.NODE_ENV === 'production'? '/vue-project':'/',
    outputDir:'myassets', // 输出路径
    assetsDir:'static', // 生成静态目录的文件夹
    runtimeCompiler: true, // 是否可以使用template模板
    parallel:require('os').cpus().length > 1, //多余1核cpu时 启动并行压缩
    productionSourceMap:false, //生产环境下 不使用soruceMap

    // https://github.com/neutrinojs/webpack-chain
    chainWebpack:config=>{
        // 控制webpack内部配置
        config.resolve.alias.set('component',path.resolve(__dirname,'src/components'));
    },
    // https://github.com/survivejs/webpack-merge
    configureWebpack:{
        // 新增插件等
        plugins:[
            new SkeletonWebpackPlugin({
                webpackConfig: {
                    entry: {
                        app: resolve('./src/skeleton.js')
                    }
                }
            })
        ]
    },
    devServer:{ // 配置代理
        proxy:{
            '/api':{
                target:'http://a.zf.cn:3000',
                changeOrigin:true
            }
        }
    },
    pages: {
        index: { // 首页入口
            entry: "./src/main.js"
        },
        other: { // 其他页面入口
            entry: './src/a.js',
            chunks:['other'] // 引入的资源
        }
    },
    // 第三方插件配置
    pluginOptions: {
        'style-resources-loader': {
            preProcessor: 'less',
            patterns: [
                // 插入全局样式
                path.resolve(__dirname,'src/assets/common.less'), 
            ],
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

骨架屏配置

import Vue from 'vue';
export default new Vue({
  components: {
    Skeleton:{
      template:`<div>skeleton</div>`
    }
  },
  template: `
    <Skeleton></Skeleton>
  `
});
1
2
3
4
5
6
7
8
9
10
11

三.封装axios

import axios from 'axios';
let baseUrl = process.env.NODE_ENV !== 'production'?'http://localhost:8080':'/';
class AjaxRequest{
  constructor(){
      this.baseURL = baseUrl; // 配置默认路径
      this.requestQueue = {}; // 缓存当前请求队列 用来操作loading的显示或者隐藏
  }
  configInterceptors(instance,url){
      // 配置请求拦截
      instance.interceptors.request.use( (config) => {
          //  loading效果 开始请求前显示loading
          if(Object.keys(this.requestQueue).length === 0){
              console.log('显示loading');
          }
          this.requestQueue[url] = config.url;
          return config;
      });
      // 配置响应拦截
      instance.interceptors.response.use(res=>{
          // 取消loading效果
          delete this.requestQueue[url];
          if(Object.keys(this.requestQueue).length === 0){
              console.log('取消loading');
          };
          if(res.status !== 200){
              return Promise.reject(res);
          }else{
              return res.data;
          }
      })

  }
  request(config){
      // 创建一个axios实例
      let instance = axios.create();
      // axios(config);
      config = {baseURL:this.baseURL,...config};
      this.configInterceptors(instance,config.url);
      return instance(config);
  }
}
export default new AjaxRequest();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Last Updated: 10/26/2019, 11:00:28 AM