強烈的文字我是新的角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()中的答案。 –