写一个config.js
文件,做为项目地址的配置。ios
1 //项目域名地址 2 const url = 'https://exaple.com'; 3 4 5 let ROOT; 6 //因为封装的axios请求中,会将ROOT打包进去,为了方便以后再也不更改,判断了当前环境,而生成的不一样的ROOT 7 if (process.env.NODE_ENV === 'development') { 8 //开发环境下的代理地址,解决本地跨域跨域,配置在config目录下的index.js dev.proxyTable中 9 ROOT = "/apis" 10 } else { 11 //生产环境下的地址 12 ROOT = url; 13 } 14 15 exports.PROXYROOT = url; //代理指向地址 16 exports.ROOT = ROOT;
这里暴露出去了两个接口,一个做为代理指向地址,也就是真正的请求地址,一个则为咱们的ajax
请求的地址。ajax
咱们将ROOT
引入咱们配置的ajax
中,再将proxyConfig.js
修改以下:axios
1 const config = require("../src/fetch/config"); //路径大家改下 2 module.exports = { 3 proxy: { 4 [config.ROOT]: { //将www.exaple.com印射为/apis 5 target: config.PROXYROOT,, // 接口域名 6 secure: false, // 若是是https接口,须要配置这个参数 7 changeOrigin: true, //是否跨域 8 pathRewrite: { 9 [`^${config.ROOT}`]: '' //须要rewrite的 10 } 11 } 12 } 13 }
参考文章segmentfault
https://segmentfault.com/a/1190000011007043api
https://blog.csdn.net/hyupeng1006/article/details/81810545跨域