Javascript &引用;提供的参数与调用目标的任何签名不匹配。”;使用get从API收集信息时
我得到了这个错误,我是angular 2的新手,所以我不能100%确定如何解决这个问题,我连接到一个测试API来返回一个包含一些伪数据的javascript对象。但是我的“this.onGet()”函数告诉我,提供的参数与调用目标的任何签名都不匹配,我似乎无法找出原因。 (本质上,我只是试图用API中的信息填充orderInfo数组,以便可以跨多个页面使用它)Javascript &引用;提供的参数与调用目标的任何签名不匹配。”;使用get从API收集信息时,javascript,angular,get,Javascript,Angular,Get,我得到了这个错误,我是angular 2的新手,所以我不能100%确定如何解决这个问题,我连接到一个测试API来返回一个包含一些伪数据的javascript对象。但是我的“this.onGet()”函数告诉我,提供的参数与调用目标的任何签名都不匹配,我似乎无法找出原因。 (本质上,我只是试图用API中的信息填充orderInfo数组,以便可以跨多个页面使用它) 感谢您的帮助:) 应用程序组件.ts import { Component, OnInit } from '@angular/c
感谢您的帮助:) 应用程序组件.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);
}
);
}
}
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;
}
);
}
}
详细信息.服务.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);
}
);
}
}
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>
调查你的
ngOnInit() {
this.onGet(); //////////nothing passed
}
其中,由于您的方法签名是
onGet(name:string)
您没有传递上述任何内容您的onGet函数需要一个字符串参数,从ngOnInit调用时没有提供该参数。我正在这样做,但它似乎不是导致问题的原因