强烈的文字我是新的角js 2和我有问题与我的代码编译。任何人都可以请帮助我吗?我真的不知道什么是错的。角度js 2错误TS2346
这是错误消息: app/car-parts.component.ts(17,27):错误TS2346:提供的参数不匹配调用目标的任何签名。
汽车parts.component.ts
import { Component } from '@angular/core';
import { CarPart } from './car-part';
import { RacingDataService } from './racing-data.service';
@Component({
selector: 'car-parts',
templateUrl: 'app/car-parts.component.html',
styleUrls: ['app/css/car-parts.component.css']
})
export class CarPartsComponent{
carParts: CarPart[];
constructor(private racingDataService: RacingDataService){}
// ngOnInit is invoked after the component is constructed
ngOnInit(){
let racingDataService = new RacingDataService();
// this.carParts = racingDataService.getCarParts();
this.racingDataService.getCarParts().subscribe(carParts => this.carParts = carParts);
}
getTotalCarParts(){
let sum = 0;
if(Array.isArray(this.carParts)){
for(let carPart of this.carParts){
sum += carPart.inStock;
}
}
return sum;
// return this.carParts.reduce((prev,curr) => prev + curr.inStock,0);
}
upQuantity(carPart){
if(carPart.quantity < carPart.inStock)
carPart.quantity++;
else{
alert("The ordered quantity exeeded the stocks");
}
}
downQuantity(carPart){
if(carPart.quantity > 0)
carPart.quantity--;
}
}
赛车data.service.ts
import { CARPARTS } from './mock';
import { Injectable } from '@angular/core';
import { CarPart } from './car-part';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class RacingDataService{
constructor(private http: Http){
}
getCarParts(){
return this.http.get('app/car-parts.json').map(response => <CarPart[]>response.json().data);
// return CARPARTS
}
}
它运行,但只显示:载入中... –
我不确定,那么你必须检查你的组件是否正在加载?在'.subscribe()'控制台中'this.carParts'。并在'subscribe()'中使用'error =>错误'并且看看你得到了什么? –
我的问题现在已解决。我忘了在问题中包含app.module.ts。问题是我把HttpModule放在@NgModule里面的声明中。感谢您在ngOnInit()中的答案。 –