【问题标题】:Error: Supplied parameters do not match any signature of call target错误:提供的参数与调用目标的任何签名都不匹配
【发布时间】:2017-04-26 23:52:34
【问题描述】:

我是 Angular 新手,我在编译代码时遇到问题。

这是错误信息: app/car-parts.component.ts(17,27): 错误 TS2346: 提供的参数与调用目标的任何签名都不匹配。

代码:

car-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--;
    }
}

racing-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
    }

}

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    我收到了这个 Angular 应用程序的类似错误:错误:无法解析 CarPartsComponent 的所有参数:(?)。这对我有帮助:

    car-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';
    }
    

    【讨论】:

      【解决方案2】:

      试试这个:

      providers中添加service

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

      然后删除行:

      let racingDataService = new RacingDataService();
      

      来自ngOnInit()

      应该是这样的:

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

      希望这对你有用。

      【讨论】:

      • 我不确定,那么您必须检查您的组件是否正在加载? .subscribe() 中的控制台 this.carParts。并在subscribe() 中使用error =&gt; &lt;any&gt; error,看看你得到了什么?
      • 我的问题现在解决了。我忘了在问题中包含 app.module.ts 。问题是我将 HttpModule 放在 @NgModule 中的声明中。感谢您在 ngOnInit() 部分的回答。
      猜你喜欢
      • 2016-02-15
      • 2016-06-09
      • 2016-11-14
      • 2017-06-08
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 2017-08-24
      相关资源
      最近更新 更多