在vue.config.js中配置如下内容
module.exports = defineConfig({
transpileDependencies: true,
lintOnSave: false,
devServer:{
port:8081,//前端项目所使用的端口
proxy: {
"/api": { // /api 匹配项,匹配拦截;
target: "http://localhost:8080/houseRental", //被请求的地址,需要被代理的地址(后台)
changeOrigin: true,//允许跨域
pathRewrite: {
//重写配置,被代理的接口会多一个‘/api’的前缀,而原本的接口是没有的,
//所以需要通过此项来将接口的前缀‘/api’转换为‘’
"^/api": "" // 此处一般是替换为vue项目名称
}
}
}
}
})
配置request.js,将baseURL设置为’/api’
const server = axios.create({
baseURL: '/api',
timeout: 10000,
headers: {
'Content-Type': 'application/json'
}
})