【问题标题】:Ionic2 Supplied parameters do not match any signature of call targetIonic2 提供的参数与调用目标的任何签名都不匹配
【发布时间】:2017-07-26 07:44:25
【问题描述】:

我试图弄清楚为什么在运行我的 ionic 2 项目时出现以下错误:

提供的参数与调用目标的任何签名都不匹配。 (第 16 行)

它指向的代码如下:

card.servce.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { Card } from "./cards";

@Injectable()
export class CardService {

    private cardsUrl = 'my_url_here';

    constructor(private http: Http) {}

    getCards(): Promise<Card[]>  {
        return this.http.get(this.cardsUrl) // Error is here
                .toPromise()
                .then(response => response.json().cards as Card[])
                .catch(...);
    }
}

不知道为什么会出现此错误,是否缺少以下参数: this.http.get(this.cardsUrl)?


Angular 核心:2.2.1

【问题讨论】:

  • 试试private cardsUrl:string = 'my_url_here';
  • @suraj 同样的问题。

标签: angularjs angular typescript ionic2 angular-http


【解决方案1】:

getCards() 返回一个完整的承诺,但根据定义的返回类型,它应该只返回一个承诺:

选项 1:

getCards(){
    return this.http.get(this.cardsUrl)
        .toPromise()
        .then(response => response.json().cards as Card[])
        .catch(...);
    }

选项 2:

getCards(): Promise<Card[]>  {
        return this.http.get(this.cardsUrl)
                .toPromise();
    }

// use this function returnResult()

returnResult(){
    this.getCards().then(response => response.json().cards as Card[])
                .catch(...);
}

【讨论】:

  • 谢谢,这个特定的实现仍然会产生同样的错误,但我会将其标记为正确,因为您的解释确实帮助我解决了它。
  • 很高兴为您提供帮助 :)
猜你喜欢
  • 1970-01-01
  • 2016-06-09
  • 2017-04-28
  • 2017-08-24
  • 2017-02-25
  • 2018-03-31
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多