2016-12-12 92 views
0

強烈的文字我是新的角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 
    } 

} 

回答

1

試試這個:

添加service提供商

@Component({ 
    ..., 
    providers: [RacingDataService] 
}) 

然後取出線:

let racingDataService = new RacingDataService(); 

ngOnInit()

它應該是這樣的:

ngOnInit(){ 
    this.racingDataService.getCarParts().subscribe(carParts => this.carParts = carParts); 
} 

希望這會爲你工作。

+0

它運行,但只顯示:載入中... –

+0

我不確定,那麼你必須檢查你的組件是否正在加載?在'.subscribe()'控制檯中'this.carParts'。並在'subscribe()'中使用'error =>錯誤'並且看看你得到了什麼? –

+0

我的問題現在已解決。我忘了在問題中包含app.module.ts。問題是我把HttpModule放在@NgModule裏面的聲明中。感謝您在ngOnInit()中的答案。 –

0

我得到了這個Angular應用程序的類似錯誤:錯誤:無法解析CarPartsComponent的所有參數:(?)。這幫助了我:

車parts.component.ts

import { Component } from '@angular/core'; 
import { CarPart } from './car-part'; 
import { RacingDataService } from './racing-data.service' 

@Component({ 
    selector: 'car-parts', 
    templateUrl: './car-parts.component.html', 
    styleUrls: ['./car-parts.component.css'] 
}) 
export class CarPartsComponent { 
    carParts: CarPart[]; 

    ngOnInit() { 
    this.carParts = this.racingDataService.getCarParts(); 
    } 

    constructor(private racingDataService: RacingDataService) {} 
} 

app.component.ts

import { Component } from '@angular/core'; 
import { RacingDataService } from './racing-data.service' 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'], 
    providers: [RacingDataService] 
}) 
export class AppComponent { 
    title = 'Welcome to racing'; 
}