2017-03-31 8 views
1

私の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。

+0

問題だけ、下にあなたのサービスを設定していなかったので、なぜあなたは代わりに '' Observable'の負荷() 'の約束を返すのですか? –

+0

確かに、私はObservableを使うことができましたが、その間にこのコードを別のスレッドから手に入れてすぐに使用しました:-)また、私はnodejsとangularjsの間でもジャグリングしています;-)。 – LucasBrazi06

答えて

1

そのあなたがプロバイダ配列

providers: [ ConfigService,////////////////change///////////////////////////// 
    { provide: APP_INITIALIZER, useFactory: (config: ConfigService) =>() => config.load(), deps: [ConfigService], multi: true } 
] 
+0

実際、エラーはなくなりました!ありがとう! – LucasBrazi06

+0

しかし、 'http'は、注入されていないかのように(未定義の)埋められておらず、load()メソッドで使われているときには失敗するようです。何か案が? – LucasBrazi06

+0

あなたは今何を得るのですか? – Aravind

関連する問題