2017-06-12 258 views
1

我想從使用Promise的服務返回數組。然後。該功能是我的服務如下:Angular 2和TypeScript Promise返回數組

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); 
    }); 
} 

和代碼返回此錯誤消息:

不能調用,其類型缺乏

任何幫助是非常讚賞,因爲我在不同的部件使用類似的邏輯呼叫簽名的表達式。

回答

1

在您的服務是不是一個功能。功能是getUsers.userList。所以你應該用this.UserService.getUsers.userList()來調用它。

+0

是的,它的工作原理!非常感謝你 –