vue.config.js配置代理

代码中全局配置一下axios的baseURL地址:

import axios from 'axios';

const instance = axios.create{

    baseURL:  '/proxyApi'

}

...

vue.config.js配置代理(提示:配置完成后,重启一下服务,使其生效)

module.exports = {

    lintOnSave: false, // 禁用eslint-loader

    devServer: {

        proxy: {

            '/proxyApi': {

                target: 'https://api-test.xxx.cn:8081',

                ws: false,

                pathRewrite: {

                    '^/proxyApi': ''

                },

                changeOrigin: true,

            },

        '/proxyApi2': {    // 多个配置

                target: 'https://api-test.yyy.cn:8081',

                ws: false,

                pathRewrite: {

                    '^/proxyApi2': '/api'

                },

                changeOrigin: true,

            }

        }

    }

}

解释说明:

/*

    proxy代理api说明:

    1. 远程服务器未处理跨域:接口为https://api-test.xxx.cn:8081

    2. 本地开发环境中的url为:http://localhost:8080

    '/proxyApi': 是自定义的本地请求时的名字

    target表示代理的服务器,即要访问的后端api服务器

    pathRewrite表示路径重写,此处是将/proxyApi重写为空

        就是将 http://localhost:8080/proxyApi 重写为 https://api-test.xxx.cn:8081

    changeOrigin: true, // 开启代理:在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题

    proxy: {

        '/proxyApi': {

            target: 'https://api-test.xxx.cn:8081',

            pathRewrite: {

            '^/proxyApi' : ''

            }

            changeOrigin: true,

        }

    } 

*/

你可能感兴趣的:(vue.config.js配置代理)