1、在manifest.json h5的配置中添加如下
"devServer": {
"port":8080,
"disableHostCheck": true,
"proxy": {
"/api": {
"target": "https://www.xxx.cn", // 需要跨域的域名
"changeOrigin": true,
"secure": false,
"pathRewrite": {
"^/api": "" //匹配请求路径里面有 /api 替换成 https://www.xxx.cn
}
}
}
}
2、在封装的请求中可以设置前置请求域名如下
// #ifdef H5
export const BASE_URL = process.env.NODE_ENV === 'production' ? 'https://www.xxx.cn' : "/api";
// #endif