2017-04-22 59 views
1

我有這樣的服務:錯誤:無效的供應商爲NgModule「的AppModule」 - 唯一供應商和類型的實例被允許

import { Injectable } from '@angular/core'; 
import { Http, Response, Headers, RequestOptions } from '@angular/http'; 
import { Observable } from 'rxjs/Rx'; 
import 'rxjs/add/operator/map'; 
import 'rxjs/add/operator/catch'; 
import { BaseUrl } from "../config/base-url.config"; 

enum HttpMethod { 
    get, 
    post, 
    patch, 
    put 
} 


@Injectable() 
export class AdminHttpService<T> { 
    baseUrl: string; 

    constructor(private _http: Http, private _baseUrl: BaseUrl) { 
     this.baseUrl = _baseUrl.getHttpBaseUrl(); 
    } 

    getCollection(relativeUrl: string): Observable<T[]>; 
    getCollection(relativeUrl: string, options: RequestOptions): Observable<T[]>; 

    getCollection(relativeUrl: string, options?: RequestOptions): Observable<T[]> { 
     return this.xhr(relativeUrl, HttpMethod.get, options); 
    } 

    get(relativeUrl: string): Observable<T>; 
    get(relativeUrl: string, options: RequestOptions): Observable<T>; 

    get(relativeUrl: string, options?: RequestOptions): Observable<T> { 
     return this.xhr(relativeUrl, HttpMethod.get, options); 
    } 

    getJson(path): Observable<T> { 
     return this._http.get(path) 
      .map((resp: Response) => resp.json()) 
      .catch((error: any) => Observable.throw(error.json().error || 'Something went wrong!')); 
    } 

    create(relativeUrl: string, data: any, options?: RequestOptions): Observable<T> { 
     return this.xhr(relativeUrl, HttpMethod.post, data, options); 
    } 

    private xhr(relativeUrl: string, httpMethod: HttpMethod, requestBody?: any, options?: RequestOptions): any { 
     var requestUrl: string = this.baseUrl + relativeUrl; 

     if (!options) { 
      options = new RequestOptions({ 
       withCredentials: true 
      }); 
     } 

     switch (httpMethod) { 

      case HttpMethod.get: 
       return this._http.get(requestUrl, options) 
        .map((resp: Response) => resp.json()) 
        .catch((error: any) => Observable.throw(error.json().error || 'Something went wrong!')); 

      case HttpMethod.post: 
       return this._http.post(requestUrl, requestBody, options) 
        .map((resp: Response) => resp.json()) 
        .catch((error: any) => Observable.throw(error.json().error || 'Something went wrong!')); 
     } 

    } 
} 

加入此服務的AppModule的供應商名單後,我收到以下錯誤:

Error: Invalid provider for the NgModule 'AppModule' - only instances of Provider and Type are allowed, got: [EventsService, MenuService, BaseUrl, ?undefined?] at SyntaxError.ZoneAwareError

的AppModule是這樣的:

, 
    providers: [ 
    EventsService, 
    MenuService, 
    BaseUrl, 
    AdminHttpService 
    ], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { } 

回答

2

拉我的頭髮分開後,我終於找到ŧ他的理由上述錯誤:

我有一個包含一個桶文件:

export * from '../../services/js-events/event.service'; 
export * from '../../services/menu/menu.service'; 
export * from '../../http/admin-http.service'; 
export * from '../../config/base-url.config'; 

不知怎的,下面有複製和應用開始拋出提到的錯誤:(這是真的很難弄清楚這一個!

export * from '../../http/admin-http.service';

因此,如果您的桶文件具有重複導出,則可能會出現此錯誤。我不知道爲什麼會在這個時候出現這種情況。如果有人知道,請告訴我。這背後肯定有一個原因。