Vue请求跨域(请求映射)

前言

这里用的 Antd pro vue,与 vue 相差不大。

在开发环境中,是可以配置请求代理的。

开发环境与打包后的实际运行环境的配置有很大区别,这里只记录开发环境。

vue.config.js

这里配置的代理路径为 /boot ,当前路径的请求,都会转发到 http://localhost:8888 上。

pathRewrite: { '/': '/', } 表示当前路径不变。

比如,当前请求为 http://127.0.0.1:8000/boot/user/login 则当前请求会被转发到 http://localhost:8888/boot/user/login

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const vueConfig = {
//************
devServer: {
// development server port 8000
port: 8000
// If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
, proxy: {
/*基础接口*/
'/boot': {
target: 'http://localhost:8888',
ws: false,
changeOrigin: true,
pathRewrite: {
'/': '/',
}
},
},
},
}
module.exports = vueConfig;

html 中的图片路径

html 总的图片路径,如果使用的路径格式符合代理规则,则当前 url 也会被代理。

比如说下面两种路径,都会被代理:

- http://127.0.0.1:8000/boot/file/server/view/20210422083822582708935283585024.jpg
- /boot/file/server/view/20210422083822582708935283585024.jpg

本文地址: https://github.com/maxzhao-it/blog/post/54174/