vue cil4 axios 跨域

99 阅读1分钟

跨域

新创建的vue是没有vue.config.js文件的,需要自行创建一个,这个文件于src同级,
其中代码如下:

module.exports = {
    // publicPath: process.env.NODE_ENV === "production" ? './' : '/api',
    devServer: {
        proxy: {
            // '/api2': {//这个优先匹配,写在前面
            //     target: 'http://192.168.100.112:8081/',
            //     https: false, // 如果是 https ,需要开启这个选项
            //     changeOrigin: true,//开启跨域
            //     pathRewrite: {
            //         '^/api2': '/'//正则匹配
            //     }
            // },
            '/api': {
                target: '  跨域的地址',//别忘记加协议!!如http://
                secure: false, // 如果是 https ,需要开启这个选项
                changeOrigin: true,//开启跨域
                pathRewrite: {
                    '^/api': '/'//正则匹配,相当于用api替换target中的地址
                }
            }
        }
    }
}

配置:
axios.defaults.baseURL = ‘’/api’;