Javascript 未捕获(承诺中):错误:DI错误角度2

Javascript 未捕获(承诺中):错误:DI错误角度2,javascript,angular,exception,dependency-injection,Javascript,Angular,Exception,Dependency Injection,谢谢你的评论,我已经编辑提供代码 有两种服务,一种是有效的,另一种是无效的 应用程序模块.ts import { NgModule, enableProdMode } from '@angular/core'; import { BrowserModule, Title } from '@angular/platform-browser'; import { routing, routedComponents } from './app.routing'; import { APP_BASE_

谢谢你的评论,我已经编辑提供代码

有两种服务,一种是有效的,另一种是无效的

应用程序模块.ts

import { NgModule, enableProdMode } from '@angular/core';
import { BrowserModule, Title } from '@angular/platform-browser';
import { routing, routedComponents } from './app.routing';
import { APP_BASE_HREF, Location } from '@angular/common';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
import { HttpModule  } from '@angular/http';
import { SampleDataService } from './services/sampleData.service';
import { TradeDataService } from './services/tradeData.service';
import './rxjs-operators';

// enableProdMode();

@NgModule({
    imports: [BrowserModule, FormsModule, HttpModule, routing],
    declarations: [AppComponent, routedComponents],
    providers: [SampleDataService, TradeDataService, Title, { provide: APP_BASE_HREF, useValue: '/' }],
    bootstrap: [AppComponent]
})
export class AppModule { }
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { TradeData } from '../models/tradeData';

@Injectable()
export class TradeDataService {

   // private url: string = 'api/items/';
    private url: string = 'api';
    constructor(private http: Http) { }

    getAllTrades(): Observable<TradeData> {
        return this.http.get(this.url + 'tradeData')
            .map((resp: Response) => resp.json())
            .catch(this.handleError);
    }

    // from https://angular.io/docs/ts/latest/guide/server-communication.html
    private handleError(error: Response | any) {
        // In a real world app, we might use a remote logging infrastructure
        let errMsg: string;
        if (error instanceof Response) {
            const body = error.json() || '';
            const err = body.error || JSON.stringify(body);
            errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
        } else {
            errMsg = error.message ? error.message : error.toString();
        }
        console.error(errMsg);
        return Observable.throw(errMsg);
    }
}
import { Component, OnInit } from '@angular/core';
import { TradeDataService } from './services/tradedata.service';
import { TradeData } from './models/tradeData';

@Component({
    selector: 'my-stock',
    templateUrl: '/partial/stocksComponent'
})

export class StocksComponent implements OnInit {
    tradeData: TradeData;
    errorMessage: string;


    constructor(private tradeDataService: TradeDataService) { }

    ngOnInit() {
        this.tradeDataService.getAllTrades()
            .subscribe((data: TradeData) => this.tradeData = data,
                error => this.errorMessage = <any>error);
    }
}
tradeData.Service.ts

import { NgModule, enableProdMode } from '@angular/core';
import { BrowserModule, Title } from '@angular/platform-browser';
import { routing, routedComponents } from './app.routing';
import { APP_BASE_HREF, Location } from '@angular/common';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
import { HttpModule  } from '@angular/http';
import { SampleDataService } from './services/sampleData.service';
import { TradeDataService } from './services/tradeData.service';
import './rxjs-operators';

// enableProdMode();

@NgModule({
    imports: [BrowserModule, FormsModule, HttpModule, routing],
    declarations: [AppComponent, routedComponents],
    providers: [SampleDataService, TradeDataService, Title, { provide: APP_BASE_HREF, useValue: '/' }],
    bootstrap: [AppComponent]
})
export class AppModule { }
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { TradeData } from '../models/tradeData';

@Injectable()
export class TradeDataService {

   // private url: string = 'api/items/';
    private url: string = 'api';
    constructor(private http: Http) { }

    getAllTrades(): Observable<TradeData> {
        return this.http.get(this.url + 'tradeData')
            .map((resp: Response) => resp.json())
            .catch(this.handleError);
    }

    // from https://angular.io/docs/ts/latest/guide/server-communication.html
    private handleError(error: Response | any) {
        // In a real world app, we might use a remote logging infrastructure
        let errMsg: string;
        if (error instanceof Response) {
            const body = error.json() || '';
            const err = body.error || JSON.stringify(body);
            errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
        } else {
            errMsg = error.message ? error.message : error.toString();
        }
        console.error(errMsg);
        return Observable.throw(errMsg);
    }
}
import { Component, OnInit } from '@angular/core';
import { TradeDataService } from './services/tradedata.service';
import { TradeData } from './models/tradeData';

@Component({
    selector: 'my-stock',
    templateUrl: '/partial/stocksComponent'
})

export class StocksComponent implements OnInit {
    tradeData: TradeData;
    errorMessage: string;


    constructor(private tradeDataService: TradeDataService) { }

    ngOnInit() {
        this.tradeDataService.getAllTrades()
            .subscribe((data: TradeData) => this.tradeData = data,
                error => this.errorMessage = <any>error);
    }
}
日志文件中有这个。 js:3064异常:未捕获(承诺中):错误:DI错误 错误:DI错误 在NoProviderError.ZoneAwareError([URL在此剪掉]zone.js:992:33) 在NoProviderError.BaseError[作为构造函数](URL在这里被截断]core.umd.js:1239:20) 在NoProviderError.AbstractProviderError[作为构造函数](此处剪下URL]core.umd.js:1365:20) 在新的NoProviderError上(此处剪下URL]core.umd.js:1405:20)
在ReflectiveInjector_u.\uThroworNull(此处剪下URL)core.umd.js:2937:23)

我们需要完整的代码来帮助您。你不能决定什么是有用的!你在使用cdn链接进行导入吗?你在使用SystemJS吗?可能dup真的很有用。我查看了yurzui的评论,发现了一个案例问题。我们需要完整的代码来帮助您。你不能决定什么是有用的!你在使用cdn链接进行导入吗?你在使用SystemJS吗?可能dup真的很有用。我看了yurzui的评论,发现了一个案例问题。