我遇到了这个错误,我 new to angular 2,所以我不确定100%确定如何解决问题,我是连接到测试API返回包含一些虚拟数据的JavaScript对象。但是我的" this.onget(("函数告诉我,所提供的参数与呼叫目标的任何签名不符,我似乎无法弄清楚原因。(本质上,我只是想用API中的信息填充OrderInfo数组,以便我可以在多个页面上使用它(
任何帮助:(
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);
}
);
}
}
delets.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)
的情况下,您没有通过上述内容
您的onget函数正在期望字符串参数,该参数在ngoninit呼叫时未提供。