使用 https://github.com/chimurai/http-proxy-middleware
- NPM install --save-dev http-proxy-middleware
- //webpack.config.JS
- var proxy = require('http-proxy-middleware');
- ...............
- devServer: {
- historyApiFallback: true,
- noInfo: true,
- overlay: true,
- proxy: {
- '/api': {// 匹配根路径
- target: 'https://******/',// 跨域要访问的地址及端口
- changeOrigin: true,
- }
- },
- }
用到的时候
- axios.get('/api/v1/dairy/list')
- .then(res => {
- // 成功回调
- console.log(res);
- }, res => {
- // 错误回调
- console.log(res);
- })
来源: http://www.bubuko.com/infodetail-2973362.html