美文网首页
Vue 实现请求转发

Vue 实现请求转发

作者: 你好岁月神偷 | 来源:发表于2021-11-12 09:26 被阅读0次

可以在 vue.config.js 中对 devServer 进行配置:

module.exports = {

    devServer: {

        host: 'localhost',  //target,  host

        port: 8080, 

        proxy: {  // proxy:{'/api':{}},代理器中设置/api,项目中请求路径为/api的替换为target

            'api': {
                target: 'http://localhost:3000',  //代理地址,这里设置的地址会代替axios中设置的baseURL

                changeOrigin: true,// 如果接口跨域,需要进行这个参数配置

                 pathRewrite: {

                     '^/api': '/'  // pathRewrite: {'^/api': '/'} 重写之后url为http://192.168.1.16:8085/xxxx 

                                  // pathRewrite: {'^/api': '/api'} 重写之后url为 http://192.168.1.16:8085/api/xxxx 

                }

            }

        }

    }

}

教学视频传送门: https://www.bilibili.com/video/BV1Dp4y147ga/?spm_id_from=333.788.recommend_more_video.-1

相关文章

网友评论

      本文标题:Vue 实现请求转发

      本文链接:https://www.haomeiwen.com/subject/jgmnzltx.html