vue-cli3 跨域配置

在项目的根目录创建vue.config.js文件

module.exports = {
  devServer: {
       open: true, //是否自动弹出浏览器页面
        proxy: {
            '/api': {
                target: 'http://localhost:5000', //API服务器的地址
                ws: true,  //代理websockets
                changeOrigin: true, // 虚拟的站点需要更管origin
                pathRewrite: {   //重写路径 比如'/api/aaa/ccc'重写为'/aaa/ccc'
                    '^/api': ''
                }
            }
        },
  }
}

如果有多个需要跨域的服务器,单独设置路径肯定是最好的,但如果只向一个服务器发送跨域请求,那么简单点就行。

module.exports = {
  devServer: {
    proxy: 'http://localhost:4000'
  }
}

你可能感兴趣的:(vue-cli3 跨域配置)