vue本地跨域解决方案

本地:http://localhost:3001/
后台:http://localhost:3002/
proxy: {
    '/vue-manage': {
        target: 'http://localhost:3001/',
        changeOrigin: true,
        pathRewrite: {
            '^/vue-manage': '',
        },
    },
},
  • target:需要代理的后台地址
  • changeOrigin:决定请求头中的host属性值,为true代表本地http://localhost:3001/,为false代表与后台一致http://localhost:3002/
  • pathRewrite:重写地址,匹配到/vue-manage后变为空字符串

前端接口统一使用/vue-manage前缀进行处理,当匹配到此前缀时,表示该接口需要进行代理,例如本地/vue-manage/test会被代理成http://localhost:3002/test

原文地址:https://www.cnblogs.com/baifangzi/p/15393683.html