[英]Return Promise in Angular with typescript as custom Data Type
[英]Angular 2 and TypeScript Promise return array
我試圖使用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);
});
}
並且代碼返回此錯誤消息:
無法調用類型缺少調用簽名的表達式
非常感謝您的幫助,因為我在不同的組件中使用了類似的邏輯。
在您的服務中, getUsers
不是函數。 getUsers.userList
是函數。 因此,您應該使用this.UserService.getUsers.userList()
調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.