1、安装http-proxy-middleware:
yarn add http-proxy-middleware
2、在'src'目录下新建'setupProxy.js'并写入:
const proxy = require('http-proxy-middleware')
module.exports = function (app) {
app.use(proxy('/api/*', {
target: ' https://easy-mock.com/mock/5ca61b139e68632dac83b143/example',
changeOrigin: true,
}))
}
3、重新启动服务,在测试组件中加入类似url:
request({
url:'/api/mock',
method: 'GET',
success: (res) => {
console.log('res',res)
}
});