vue 跨域问题解决

 1.前提是服务器设置了跨域支持

2. 在vue.config.js 里面写上

const path = require('path');
// 获取路径方法
function resolve(dir) {
    return path.join(__dirname, dir);
}
const config = {
    publicPath: './',
    devServer:{ 
        open: true,
        host: 'localhost',
        port: 8000,
        https: false,
        //以上的ip和端口是我们本机的;下面为需要跨域的
        proxy: { //配置跨域
            '/api': {
                target: 'http://真实IP:8091/', //这里后台的地址模拟的;应该填写你们真实的后台接口
                ws: true,
                changOrigin: true, //允许跨域
                pathRewrite: {
                    '^/api': '' //请求的时候使用这个api就可以
                }
            }
        } 
    },
    chainWebpack: config => {
        config.resolve.alias
            .set("@", resolve('src'))
            .set("@views", resolve("src/views"))
            .set("@api", resolve("src/api"))
            .set("@assets", resolve("src/assets"))
            .set("@c", resolve("src/components"));
    },
    productionSourceMap: false,
}
module.exports = config;

配置地址为 /api

猜你喜欢

转载自blog.csdn.net/Allure_LoveU/article/details/121097206