【问题标题】:Reading application settings from JSON file in angular application从 Angular 应用程序中的 JSON 文件读取应用程序设置
【发布时间】:2019-10-18 22:17:20
【问题描述】:

我已经实现了从我的 Angular 应用程序的 JSON 文件中读取的应用程序配置,以便在部署后轻松配置它。 我们使用 Octopus 部署工具来部署 Angular 应用程序。如您所知,他的 enviornment.ts 文件将生成为 js 文件,并且 通过读取 JS 文件来操作 Octopus 中的设置非常困难。因此,我创建了 config.JSON 文件,该文件将输出到 dist 文件夹和 Octopus deploy 可以使用 JSON 文件在需要时进行更改。

当前应用程序正在从环境 ts 文件中读取值,而后者又在读取 Config.JSON 文件中设置的值。

我想知道我的方法是否正确?

配置.JSON

{
    "settings": {
        "production": false,
        "userIdleMinutes": "10",
        "corePingIntervalSeconds": "10",
        "baseUrl": "http://localhost:57973",
        "loginUrl": "/Login",
        "adminUrl": "/Admin"
    }
}

环境.ts

import config from '../assets/config.json'

// The file contents for the current environment will overwrite these during build.
// The build system defaults to the dev environment which uses `environment.ts`, but if you do
// `ng build --env=prod` then `environment.prod.ts` will be used instead.
// The list of which env maps to which file can be found in `.angular-cli.json`.

export const environment = {
    production: config.settings.production,
    baseUrl: config.settings.baseUrl,
    loginUrl:config.settings.loginUrl,
    adminUrl: config.settings.adminUrl,
    userIdleMinutes: config.settings.userIdleMinutes,
    corePingIntervalSeconds: config.settings.corePingIntervalSeconds 
};

【问题讨论】:

  • 我不明白你的方法为什么不正确。 a) 有效; b) 它允许从 Octopus 注入依赖于环境的值。

标签: json angular octopus-deploy


【解决方案1】:

如果您使用octopus,而不是这样做,请在您的 environment.prod.ts 中添加一些标记并在每个环境中将它们替换为 octopus

(现在我无法向您展示如何操作,但如下所示)

environment.prod.ts

export const environment = {
    production: #{production},
    baseUrl: "#{baseUrl}",
    //... rest of your configs
};

然后添加一个步骤,将这些变量替换为每个部署时 main.*.js 文件中 environment 中定义的变量

【讨论】:

  • 我将配置移动到 Json 文件的原因是为了简单。否则我需要在 Octopus 中编写一个脚本来读取 js 文件中的值。我所知道的是,Octopus 可以通过最少的操作轻松地从 Json 文件中读取
  • 您不需要脚本,它是章鱼中步骤的标准部分,所有内容都来自您在章鱼中的环境变量octopus.com/docs/deployment-process/configuration-features/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
相关资源
最近更新 更多