【问题标题】:Externalize angular configuration file ( config.json )外部化角度配置文件( config.json )
【发布时间】:2020-08-31 15:10:48
【问题描述】:

我有一个在 jboss 应用服务器上运行的 Angular 应用程序(war 格式)。我的实际架构是:前端应用程序(angular)和后端应用程序(java),前端使用 src/conf/config.json 文件中提供的 rest api 链接回调服务。 这是我的应用配置文件:

{
"restApiUrl": "https://jboss_host:8443/back/rest/",
"ldapAuthentication" : true
}

所以,我现在的需要(客户需要)是将此配置文件外部化以轻松更改其余 api 链接,而无需进行重建或重新部署过程。

我不能在我的架构中使用任何其他元素(revers-proxy、conf server),因为它将成为客户端生产或 api 数据库(数据库连接由后台服务提供)的脑筋急转弯。

请给点建议。

【问题讨论】:

  • 创建一个服务来加载你的配置并将其挂接到 Angular 的 APP_INITIALZIER
  • 感谢重播,需要从磁盘加载(应用服务器中的某个位置)?这就是我卡住的地方
  • 同样的事情,您将使用您的服务器端(rest api)通过 java 从磁盘中获取配置信息,然后将其挂接到 angular 的 APP_INITIALIZER

标签: angular automation configuration devops reverse-proxy


【解决方案1】:

如果您不想通过 http 加载配置文件,请尝试使用 config.js 文件。

config.json 重命名为 config.js 并将内容更改为:

var config = {
  "restApiUrl": "https://jboss_host:8443/back/rest/",
  "ldapAuthentication" : true
}

然后将配置文件包含在您的应用程序中的index.html 中:

<script type="text/javascript" src="config.js"></script>

在 Angular 应用程序中,您可以通过 (&lt;any&gt;window).config 访问此配置。

为了更方便使用创建ConfigServicelike:

@Injectable({providedIn: 'root'})
export class ConfigService {
  getConfig(): Config { // You should create some model of your config too or you can use 'any'
    return (<any>window).config;
  }
}

【讨论】:

    【解决方案2】:

    @mpstv 感谢您的重播。 通过使用 java servlet 的强大功能,我能够解决这个问题。解决方案是将 WEB-INF 文件夹( servlet.class + web.xml )添加到项目战中,该文件夹将从文件系统中收取配置文件并将其提供给 angular,同时 HTTP 调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 2019-08-12
      • 2019-07-19
      • 2016-02-19
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      相关资源
      最近更新 更多