上文做好了nginx的反向代理处理前端跨域的问题,所以我想试着在本地开发的时候使用线上的数据试试,踩了个小坑,特此记录一下。
依照惯例,代码解释都在注释里面:
module.exports = {
// baseUrl: './', // 基本路径
productionSourceMap: false, // 生产环境是否生成 sourceMap 文件
// 服务器端口号
devServer: {
port: 8000,
proxy: {
'/api/': {
target: "https://zhulijun.club", // 目标地址,可加端口(不能直接写接口名,否则无效,例如:https://zhulijun.club/api/)
// changeOrigin: true, // 跨域,默认为true
// secure: false, // 本地配置https证书需要设置
pathRewrite: {
'^/api': '/api/' // 目标接口,target填写的是根目录
}
}
}
},
}
说明:target中我之前配置写的是https://zhulijun.club/api,于是npm start启动项目各种不起作用,后来多次尝试才知道query需要在pathRewrite字段里面写,长了个记性。
网友评论