Angular 2和TypeScript承诺返回数组
我正在尝试使用Promise.Then从服务返回数组。我的服务功能如下:Angular 2和TypeScript承诺返回数组,angular,typescript,es6-promise,Angular,Typescript,Es6 Promise,我正在尝试使用Promise.Then从服务返回数组。我的服务功能如下: userList: Users[] = []; getUsers = { userList: (): Promise<Users[]> => { let headers = new Headers(); headers.append('Content-Type', 'application/x-www-form-urlencoded'); head
userList: Users[] = [];
getUsers = {
userList: (): Promise<Users[]> => {
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
headers.append('Authorization', 'Bearer ' + this.authenticationService.token);
return this.http.get(this.authenticationService.url + '/tournament/getGames', {
headers: headers
}).map((response: Response) => {
var status = response.json().status;
console.log(response.json().status);
if (status) {
this.userList = response.json().users;
console.info(response.json().users);
return this.userList;
}
else {
return null;
}
}).toPromise();
}
}
恩戈尼特内部:
this.getData().then(() => this.isDataAvailable = true);
getData函数:
getData(): Promise<Users[]> {
return this.UserService.getUsers().then(users => {
this.usersList= users;
console.log(this.usersList);
});
}
getData():承诺{
返回此.UserService.getUsers()。然后(users=>{
this.usersList=用户;
console.log(this.usersList);
});
}
代码将返回此错误消息:
无法调用其类型缺少调用签名的表达式
非常感谢您的帮助,因为我在不同的组件中使用了类似的逻辑。在您的服务中
getUsers
不是一个函数getUsers.userList
是函数。因此,您应该使用this.UserService.getUsers.userList()调用它
getData(): Promise<Users[]> {
return this.UserService.getUsers().then(users => {
this.usersList= users;
console.log(this.usersList);
});
}