angular - 错误: Invalid provider for the NgModule 'AppModule' - only instances of Provider and Type are allowed

标签 angular

我有这项服务:

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 { }

最佳答案

费了九牛二虎之力终于找到了上述错误的原因:

我有一个桶文件,其中包含:

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';

因此,如果您的桶文件有重复导出,则可能会出现此错误。我不知道为什么会在这个时候发生这种情况。如果有人知道,请告诉我。这背后一定有原因。

关于angular - 错误: Invalid provider for the NgModule 'AppModule' - only instances of Provider and Type are allowed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43563057/

相关文章:

node.js - Hyperledger-Composer:为什么使用 Node.js 而不是 Angular?

javascript - 下拉列表 - 按值绑定(bind)模型 - Angular 2

css - 未满足的同伴依赖

javascript - 在 OnInit、OnDestroy 上添加和删除 css 样式

angular - CanDeactivate 确认消息

node.js - 在微服务应用程序中实现 websockets

angular - 在 Angular 中 -> 如何检查用户是否具有使用基于角色的访问权限且角色保存在数据库中的权限

angular - 无法为 ngx-bootstrap datepicker 动态设置属性

javascript - Date.now() 返回错误的日期

angular - 如何在 angular-gridster2 中的 2 'grids' 之间拖动?