在vue项目开发的过程中,在和后台联调或者使用mock-server的时候,有时候会涉及到跨域的问题,需要去设置代理。vue设置代理的方式很简单:

1、在vue项目的根目录下,新建vue.config.js文件,和package.json处于同一级目录。(vue.config.js是一个可选的配置文件,在vue项目的根目录下(和package.json平级)它会被 @vue/cli-service 自动加载,所以不需要做其他的导入导出,vue.config.js还可以使用package.json中的vue字段代替,但是手写json字符串好像有点.........)

2、然后在vue.config.js,做出如下配置

module.exports = {

    devServer: {
        proxy: {
            '/task': {
                target: 'http://127.0.0.1:6868', //后台接口的服务地址
                changeOrigin: true, //changes the origin of the host header to the target URL 设置是否将host更换为代理url
                ws: true, //websocket是否代理
                secure: false, //true/false, if you want to verify the SSL Certs,https协议的情况下为true
                pathRewrite: { //object/function, rewrite target's url path. Object-keys will be used as RegExp to match paths. 重写url路径
                    '^/task': '/task' //需要代理的路径
                }
            }
        }
    }
}

这样就可以在vue项目中的代理就设置好了。

Logo

前往低代码交流专区

更多推荐