2016-12-12 95 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'; 
}