Angular

Angular 的 proxy-config.json 代理配置

2022-01-24  本文已影响0人  survivorsfyh

首先,在项目工程的根目录下创建 proxy-config.json 文件并初始化其配置相关,具体 code 如下:

{
  "/api_test": {
    "target": "https://xxxxxx/chuke-api",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true
  },
  "/api": {
    "target": "https://xxxxxxxxxxxxxx",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true,
    "pathRewrite": {
      "^/api": ""
    }
  },
  "/osce_debug": {
    "target": "https://xxxxxxxx.org.cn",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true
  },
  "/osce_Release": {
    "target": "https://xxxxxxx.na.com",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true,
    "pathRewrite": {
      "^/osce": ""
    }
  },
  "/iMed": {
    "target": "https://xxxxxxxxxx",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true,
    "pathRewrite": {
      "^/iMed": ""
    }
  }
}

其次,在 angular.json 文件中配置 proxyConfig 相关,具体 code 如下:
在 serve 对象中的 options 集合下新增 proxyConfig,其中 value 值即创建的代理文件名称 proxy-config.json

"serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "atheory-basic-pe:build",
            "proxyConfig": "proxy-config.json"
          },
          "configurations": {
            "production": {
              "browserTarget": "theory-basic-pe:build:production"
            },
            "development": {
              "browserTarget": "theory-basic-pe:build:development"
            }
          },
          "defaultConfiguration": "development"
        },

最后,重启项目 npm start 后调用接口检测即可.


以上便是此次分享的全部内容,希望能对大家有所帮助!

上一篇 下一篇

猜你喜欢

热点阅读