本地开发中困扰许久的跨域解决方法: proxy 代理
1.在根目录下新建proxy.config.json
{
"/api": {
"target": "http://roastapp.test",//代理域名或端口号
"secure": false,//是否https
"logLevel": "debug",
"changeOrigin": true//是否跨域
}
}
2.修改配置文件package.json
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config proxy.config.json",//运行配置proxy.config.json
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
3.运行npm run start
控制台出现:
[HPM] GET /api/v1/heroes -> http://roastapp.test