【问题标题】:Angular 2 and TypeScript Promise return arrayAngular 2 和 TypeScript Promise 返回数组
【发布时间】: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


    【解决方案1】:

    在您的服务中,getUsers 不是函数。 getUsers.userList 是函数。所以你应该用this.UserService.getUsers.userList()来调用它。

    【讨论】:

      猜你喜欢
      • 2017-09-25
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 2019-12-19
      • 2018-09-08
      • 2018-07-05
      相关资源
      最近更新 更多