2017-03-31 4 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