需求:需要把localhost:4200/api上的所有调用都转给运行在localhost:8080上的服务器
可采取以下步骤:
1.在项目内新建proxy.config.json文件
2.在新建的代理配置文件中输入 和webpack.config.js类似 详细配置可以去官网
{
"/api": {
"target": "http://localhost:8080",
"secure": false
}
}
3.修改angular.json 中配置项
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"proxyConfig": "proxy.config.json" //代理配置 文件路径
},
"configurations": {
"production": {
"browserTarget": "angular-web-demo:build:production"
},
"development": {
"browserTarget": "angular-web-demo:build:development"
}
},
"defaultConfiguration": "development"
}