感谢您编辑的评论以提供代码。
有两个服务可行,第二个TradeDataService不可用。
app.module.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 { }
tradeData.Service.ts
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);
}
}
stocks.component.ts
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.ts
从@ angular / core&#39;;
导入{Component}export class TradeData {
id: number;
firm: string;
market: string;
contract: string;
clientCode: string;
amount: number;
currency: string;
userId: string;
tradeDate: string;
maturityDate: string;
orderCount?: number;
//orders?: Order[];
ordersTotal?: number;
}
export class Order {
product: string;
price: number;
quantity: number;
orderTotal ?: number;
}
日志文件中包含此内容。 core.umd.js:3064 EXCEPTION:未捕获(在承诺中):错误:DI错误 错误:DI错误 at NoProviderError.ZoneAwareError([URL Snipped here] zone.js:992:33) 在NoProviderError.BaseError [作为构造函数](URL Snipped here)core.umd.js:1239:20) at NoProviderError.AbstractProviderError [as constructor](URL Snipped here)core.umd.js:1365:20) at new NoProviderError(URL Snipped here)core.umd.js:1405:20) 在ReflectiveInjector _._ throwOrNull(URL Snipped here)core.umd.js:2937:23)