在vue-cli中新建vue.config.js文件
module.exports = {
//publicPath: './',
//runtimeCompiler: true,
devServer: {
// 设置代理
proxy: {
'/api': {
target: 'http://localhost:8000',
changeOrigin: true,//设置是否跨域
pathRewrite: {
'^/api':''
}
},
}
}
}
pathRewrite为设置重写
pathRewrite: {
'^/api':''
}
设置之后需要重启端口,之后对vue端口下的/api/ 就会向 target发送请求。
axios.get("/api/data.json")
实际上就是
axios.get("http://localhost:8000/data.json")
从而实现跨域。