美文网首页Angular
Angular 的 proxy-config.json 代理配置

Angular 的 proxy-config.json 代理配置

作者: survivorsfyh | 来源:发表于2022-01-24 10:59 被阅读0次

首先,在项目工程的根目录下创建 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 后调用接口检测即可.


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

相关文章

网友评论

    本文标题:Angular 的 proxy-config.json 代理配置

    本文链接:https://www.haomeiwen.com/subject/lxkbhrtx.html