webpack 和 vite
webpack
// ... 其他配置
devServer: {
proxy: {
'/api': {
target: 'http://target.com',
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
}
}
Vite
// vite.config,js
export default {
// ... 其他配置
proxy: {
'/api': {
target: 'http://target.com',
changeOrigin: true,
rewrite: path => path.replace(/^\/api/, '') // 这里是将 /api 替换为空字符串
}
}
}
重新编辑的问题 changOrigin: true
如何定义 /api ?
它应该是这样的
import axios from "axios";
export const service = axios.create({
baseURL: "/api",
timeout: 10000,
});
import {
service
} from './request'
// 获取本机账号信息
export const getUserData = () => {
return service({
url: "/getCurrentSummoner",
method: "get",
});
}
配置完后尝试使用 webPack
或 Vite
启动项目测试