1、在跟目录中创建配置文件:proxy.config.json ,文件内容如下:

{
  "/api": {
    "target": "http://192.168.0.200:10091",
    "secure": false,
    "pathRewrite": {
      "^/api": ""
    }
  }
}

 

2、修改 package.json 配置文件内容,内容修改如下:

"start": "ng serve --proxy-config proxy.config.json",

 

3、启动项目

npm run start

 

3、angular http 请求:

this.http.get("/api/queryitem").subscribe((res: any) => {
      console.log(res);
    });

 

请求的时候,会自动将 /api 替换成 http://192.168.0.200:10091,在浏览器调试中网络监控中显示的会依旧是网站自身的域名。

 

版权声明:本文为cnwcl原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/cnwcl/p/13803491.html