2017-06-12 54 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

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