如何更新.json文件而无需在Angular CLI中重建?

Tom*_*hen 5 javascript json angular-cli ng2-translate angular

我正在使用angular cli建立一个包含多种语言的网站。我使用ng2-translate进行翻译。

我导入在angular-cli.json中定义的languages.json文件:

"assets": [
    "assets",
    "fonts",
    "languages.json"
  ],
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我使用require检索数据:

this.languages = require('../../../../languages.json');
Run Code Online (Sandbox Code Playgroud)

加载json文件有效。部署后,我尝试更改(添加或删除)languages.json中的某些语言,但是即使json文件在dist中更新,更改也不会显示在网站上。我可以更新网站的唯一方法是更改​​原始json文件,再构建应用程序的另一个版本并再次部署。是否可以通过仅更新dist中的json文件来更改网站?

为了澄清起见,languages.json包含一个语言代码数组,用于定义网站的语言选择器中显示的语言。我希望使用此配置来更改网站中显示的语言,而无需重建应用程序。

这是我的package.json:

{
"name": "myApp",
"version": "1.0.0",
"license": "MIT",
"scripts": {
  "ng": "ng",
  "start": "ng serve",
  "dev": "ng serve --environment=dev",
  "acc": "ng serve --environment=acc",
  "prod": "ng serve --environment=prod",
  "build:dev": "ng build --environment=dev",
  "build:acc": "ng build --environment=acc",
  "build:prod": "ng build --environment=prod",
  "build": "ng build",
  "test": "ng test",
  "lint": "ng lint",
  "e2e": "ng e2e"
},
"private": true,
"dependencies": {
  "@angular/animations": "^4.0.2",
  "@angular/common": "^4.0.0",
  "@angular/compiler": "^4.0.0",
  "@angular/compiler-cli": "^4.0.0",
  "@angular/core": "^4.0.0",
  "@angular/forms": "^4.0.0",
  "@angular/http": "^4.0.0",
  "@angular/platform-browser": "^4.0.0",
  "@angular/platform-browser-dynamic": "^4.0.0",
  "@angular/platform-server": "^4.0.0",
  "@angular/router": "^4.0.0",
  "angular2-localstorage": "git+https://github.com/zoomsphere/angular2-localstorage.git#master",
  "core-js": "^2.4.1",
  "foundation-sites": "^6.3.1",
  "ng2-translate": "^5.0.0",
  "rxjs": "^5.1.0",
  "zone.js": "^0.8.5"
},
"devDependencies": {
  "@angular/cli": "1.0.0",
  "@types/jasmine": "2.5.46",
  "@types/node": "~7.0.12",
  "codelyzer": "~3.0.0-beta.4",
  "jasmine-core": "~2.5.2",
  "jasmine-spec-reporter": "~3.2.0",
  "karma": "~1.5.0",
  "karma-chrome-launcher": "~2.0.0",
  "karma-cli": "~1.0.1",
  "karma-jasmine": "~1.1.0",
  "karma-jasmine-html-reporter": "^0.2.2",
  "karma-coverage-istanbul-reporter": "^1.0.0",
  "protractor": "~5.1.0",
  "ts-node": "~3.0.2",
  "tslint": "~4.5.1",
  "typescript": "^2.2.2"
}
Run Code Online (Sandbox Code Playgroud)

Ren*_*d A 6

看来我现在有完全相同的问题,并且我使用了本教程:https : //aclottan.wordpress.com/2016/12/30/load-configuration-from-external-file/

据我了解,您需要有一个“外部”变量,您可以在不更改应用程序代码和重新构建的情况下对其进行修改?

如果是这样,我在这里所做的,适用于您的示例:

  • 将您的languages.json文件保留在“ assets”文件夹中,以使其在dist文件夹中可用。

在本演示中,我将以此为例:

{
  "languages": ["en", "fr"]
}
Run Code Online (Sandbox Code Playgroud)
  • 在指向languages.json的环境文件(environments.ts和environments.prod.ts)中添加一个languageFile属性:

environment.ts:

export const environment = {
    production: false,
    languageFile: 'assets/languages.json'
};
Run Code Online (Sandbox Code Playgroud)

environment.prod.ts:

export const environment = {
    production: true,
    languageFile: 'assets/languages.json'
};
Run Code Online (Sandbox Code Playgroud)
  • 创建一个表示您在JSON文件language.json中所拥有内容的类

举个例子:

{
  "languages": ["en", "fr"]
}
Run Code Online (Sandbox Code Playgroud)
  • 创建一个将加载此配置的服务:
export const environment = {
    production: false,
    languageFile: 'assets/languages.json'
};
Run Code Online (Sandbox Code Playgroud)
  • 在主要模块(通常是AppModule)中,添加以下导入:
export const environment = {
    production: true,
    languageFile: 'assets/languages.json'
};
Run Code Online (Sandbox Code Playgroud)
  • 仍然是您的主模块,添加一个加载功能(在模块外部):
export class Configuration {
  languages: string[];
}
Run Code Online (Sandbox Code Playgroud)
  • 仍在您的主模块中,添加以下2个提供程序:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Configuration } from './configuration';

@Injectable()
export class ConfigService {
  private config: Configuration;

  constructor(private http: Http) {
  }

  load(url: string) {
    return new Promise((resolve) => {
      this.http.get(url).map(res => res.json())
        .subscribe(config => {
          this.config = config;
          resolve();
        });
    });
  }

  getConfiguration(): Configuration {
    return this.config;
  }

}
Run Code Online (Sandbox Code Playgroud)
  • 您现在应该可以使用ConfigService加载语言。

这里以组件为例。

import { APP_INITIALIZER } from '@angular/core';
import { HttpModule } from '@angular/http';
import { ConfigService } from './config.service';
import { environment } from '../environments/environment';
Run Code Online (Sandbox Code Playgroud)

就我而言,似乎必须重新加载服务器才能进行帐户修改。

希望它会有所帮助:)

PS:对于此消息中最终出现的英语或技术错误,我们深表歉意(我在午餐时间很快写了出来)