【发布时间】:2017-06-12 20:18:47
【问题描述】:
我正在尝试使用 Promise.Then 从服务中返回一个数组。功能是我的服务如下:
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();
}
}
然后我在 administrator.component.ts 中使用它来获取用户列表:
usersList: Users[] = [];
在 ngOnInit 内部:
this.getData().then(() => this.isDataAvailable = true);
getData 函数:
getData(): Promise<Users[]> {
return this.UserService.getUsers().then(users => {
this.usersList= users;
console.log(this.usersList);
});
}
并且代码正在返回此错误消息:
无法调用类型缺少调用签名的表达式
非常感谢任何帮助,因为我在不同的组件中使用了类似的逻辑。
【问题讨论】:
标签: angular typescript es6-promise