私のangular2アプリケーションでconfig.jsonファイルをロードしたかったのですが、フロントエンドに上記のエラーがありました。Angular2でconfig.jsonをロードする:エラーを取得する:NoProviderError.ZoneAwareErrorでDIエラー
アプリの\は
import { ConfigService } from './service/config-service';
@NgModule({
providers: [
{ provide: APP_INITIALIZER, useFactory: (config: ConfigService) =>() => config.load(), deps: [ConfigService], multi: true }
]
})
アプリ\サービス\コンフィグ-service.tsに
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
@Injectable()
export class ConfigService {
private _config: Object;
constructor(private http: Http) {
console.log("Config Created");
}
load() {
// Get the Config
return new Promise(function(fulfill, reject) {
// Get the conf
this.http.get('/app/config.json') // path of your config.json file
.map(res => res.json())
.subscribe(config => {
this._config = config;
fulfill(true);
});
});
}
getCentralSystemServer() {
return this._config["CentralSystemServer"];
}
}
私にしてみましょうをapp.module.ts:私がやっていることここで
あなたが同じ種類の問題に遭遇したかどうかを知る。
ありがとう、 Serge。
問題だけ、下にあなたのサービスを設定していなかったので、なぜあなたは代わりに '' Observable'の負荷() 'の約束を返すのですか? –
確かに、私はObservableを使うことができましたが、その間にこのコードを別のスレッドから手に入れてすぐに使用しました:-)また、私はnodejsとangularjsの間でもジャグリングしています;-)。 – LucasBrazi06