“提供的参数不匹配呼叫目标的任何签名。”而使用获取从API收集信息

“提供的参数不匹配呼叫目标的任何签名。”而使用获取从API收集信息

问题描述:

我得到这个错误,我角2所以我不是100%肯定如何解决这个问题,我连接到测试API返回包含一些虚拟数据的JavaScript对象。但我的“this.onGet()”函数告诉我,提供的参数不匹配任何调用目标的签名,我似乎无法弄清楚为什么。 (基本上我只是想填充OrderInfo的阵列与来自API的信息,所以我可以用它在多个页面)

任何帮助表示赞赏:)“提供的参数不匹配呼叫目标的任何签名。”而使用获取从API收集信息

App.component.ts

import { Component, OnInit } from '@angular/core'; 
import { DetailsService } from './details.service'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'], 
    providers: [DetailsService] 
}) 

export class AppComponent implements OnInit { 
    orderInfo = [ 
     { 
      name: 'Test' 
     } 
    ]; 
    constructor(private detailsService: DetailsService) { 
    } 

    ngOnInit() { 
     this.onGet(); 
    } 

    onGet(name: string) { 
     this.detailsService.getDetails() 
      .subscribe(
       (orderData: any[]) => { 
        this.orderInfo.push({ 
         name: name 
        }); 
        console.log(orderData); 
       } 
      ); 
    } 
} 

details.service.ts

import {Injectable} from '@angular/core'; 
import { Http, Response } from '@angular/http'; 
import 'rxjs/Rx'; 

@Injectable() 
export class DetailsService { 
    constructor(private http: Http) {} 
    getDetails() { 
     return this.http.get('http://swapi.co/api/people/1/?format=json', '') 
      .map(
       (response: Response) => { 
        const orderData = response.json(); 
        return orderData; 
       } 
     ); 

    } 
} 

签名HTTP GET方法是

get(url: string, options?: RequestOptionsArgs) : Observable<Response> 

你传递一个额外的字符串参数

getDetails() { 
     ///////////////removed below single quotes 
     return this.http.get('http://swapi.co/api/people/1/?format=json') 
      .map(
       (response: Response) => { 
        const orderData = response.json(); 
        return orderData; 
       } 
     ); 

看着你

ngOnInit() { 
     this.onGet(); //////////nothing passed 
    } 

那里为你的方法签名是onGet(name:string)你不传递任何如上

+0

我正在这样做,但它似乎并不是导致问题的原因 –

您的OnGet函数需要一个字符串参数,它不是在从ngOnInit调用时提供。