vue2.0 配置跨域请求

vue2.0跨域

使用webpack-merge区分生成环境和开发环境,在项目根目录下新建config文件,并新建如下文件:
dev.env.js (开发环境)
prod.env.js (生产环境)
index.js (用于定义开发环境和生产环境所需要的参数)

dev.env.js (开发环境)

'use strict'
// 该插件是用来合并对象,也就是配置文件用的
const merge = require('webpack-merge')
const prodEnv = require('./prod.env')

// 将两个配置对象合并导出,NODE_ENV是一个环境变量,指定development环境
module.exports = merge(prodEnv, {
 NODE_ENV: '"development"'
})

prod.env.js

// 导出一个对象,NODE_ENV是一个环境变量,指定production环境
'use strict'
module.exports = {
 NODE_ENV: '"production"'
}

index.js

'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.

// 用于处理路径统一的问题
const path = require('path')

module.exports = {
 // 开发环境的配置
 dev: {
   // Paths
   assetsSubDirectory: 'static',                   // 静态资源文件夹
   assetsPublicPath: '/',                          // 发布路径
   // 一般解决跨域请求api
   proxyTable: {
       '/api': {
           target: 'http://api.douban.com/v2',     // 目标url
           changeOrigin: true,                     // 是否跨域
           pathRewrite: {
               '^/api': ''                         // 可以使用 /api 等价于 http://api.douban.com/v2
           }
       }
   },

   // Various Dev Server settings
   host: 'localhost', // can be overwritten by process.env.HOST
   port: 8080,                 // dev-server的端口号,可以自行更改
   autoOpenBrowser: false,     // 是否自定代开浏览器
   errorOverlay: true,         // 查询错误
   notifyOnErrors: true,       // 通知错误
   poll: false,                // poll轮询,webpack为我们提供devserver是可以监控文件改动的,有些情况下却不能工作,可以设置一个轮询解决

   
   /**
    * Source Maps
    */

   // https://webpack.js.org/configuration/devtool/#development
   devtool: 'cheap-module-eval-source-map',        // webpack用于方便调试的配置

   // If you have problems debugging vue-files in devtools,
   // set this to false - it *may* help
   // https://vue-loader.vuejs.org/en/options.html#cachebusting
   cacheBusting: true,       // devtool的配置当文件名插入新的hash导致清除缓存时是否生成source maps,默认为true

   cssSourceMap: true        // 是否开启cssSourceMap
 },
 // 生产编译环境下的一些配置
 build: {
   // 下面是相对路径的拼接
   index: path.resolve(__dirname, '../dist/index.html'),

   // 下面定义的是静态资源的根目录 也就是dist目录
   assetsRoot: path.resolve(__dirname, '../dist'),
   assetsSubDirectory: 'static',
   assetsPublicPath: '/',          // 下面定义的是静态资源的公开路径,也就是真正的引用路径

   /**
    * Source Maps
    */

   productionSourceMap: true,
   // https://webpack.js.org/configuration/devtool/#production
   devtool: '#source-map',

   // Gzip off by default as many popular static hosts such as
   // Surge or Netlify already gzip all static assets for you.
   // Before setting to `true`, make sure to:
   // npm install --save-dev compression-webpack-plugin
   productionGzip: false,                      // 是否在生产环境中压缩代码,如果要压缩必须安装compression-webpack-plugin
   productionGzipExtensions: ['js', 'css'],    // 定义要压缩哪些类型的文件

   // Run the build command with an extra argument to
   // View the bundle analyzer report after build finishes:
   // `npm run build --report`
   // Set to `true` or `false` to always turn it on or off
   bundleAnalyzerReport: process.env.npm_config_report     // 是否开启打包后的分析报告
 }
}

vue3.0跨域

vue3.0 直接在根目录下新建一个config.js

const path = require("path");
const resolve = function(dir) {
    return path.join(__dirname, dir);
};
module.exports = {
    publicPath: process.env.NODE_ENV === "production" ? "/dist/" : "./",
    outputDir: "dist",
    assetsDir: "static",
    lintOnSave: true, // 是否开启eslint保存检测
    productionSourceMap: false, // 是否在构建生产包时生成sourcdeMap
    chainWebpack: config => {
        config.resolve.alias
        .set("@", resolve("src"))
        .set("@s", resolve("src/service")); /* 别名配置 */
        config.optimization.runtimeChunk("single");
    },
    devServer: {
        // host: "localhost",
        /* 本地ip地址 */
        //host: "192.168.1.63",
        host: "192.168.1.63", //局域网和本地访问
        port: "8080",
        hot: true,
        /* 自动打开浏览器 */
        open: true,
        overlay: {
            warning: false,
            error: true
        },
        /* 跨域代理 */
        proxy: {
            "/api": {
                /* 目标代理服务器地址 */
                target: "http://192.168.1.63:8052", //
                /* 允许跨域 */
                changeOrigin: true,
                ws: true,
                pathRewrite: {
                    "^/api": ""
                }
            }
        }
    }
};
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值