Ionic2提供的参数与呼叫目标的任何签名都不匹配

时间:2017-03-06 12:25:31

标签: angularjs angular typescript ionic2 angular-http

我想弄清楚为什么在运行我的离子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)

error

Angular Core:2.2.1

1 个答案:

答案 0 :(得分:1)

getCards()返回一个完整填充的promise但是根据定义的返回类型,它应该只返回一个promise:

选项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(...);
}