简体   繁体   English

Angular 2和TypeScript Promise返回数组

[英]Angular 2 and TypeScript Promise return array

I am trying to return an array from a service using Promise.Then. 我试图使用Promise.Then从服务返回一个数组。 The function is my service is follow: 功能是我的服务如下:

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

    }
}

then I am using this in administrator.component.ts to get the Users list: 然后我在administrator.component.ts中使用它来获取“用户”列表:

usersList: Users[] = [];

inside ngOnInit: 在ngOnInit内部:

this.getData().then(() => this.isDataAvailable = true);

getData function: getData函数:

 getData(): Promise<Users[]> {
    return this.UserService.getUsers().then(users => {
        this.usersList= users;
        console.log(this.usersList);
    });
}

and the code is returning this error message: 并且代码返回此错误消息:

cannot invoke an expression whose type lacks a call signature 无法调用类型缺少调用签名的表达式

Any help is much appreciated as I use a similar logic in a different component. 非常感谢您的帮助,因为我在不同的组件中使用了类似的逻辑。

In your service getUsers is not a function. 在您的服务中, getUsers不是函数。 getUsers.userList is the function. getUsers.userList是函数。 So you should call it with this.UserService.getUsers.userList() . 因此,您应该使用this.UserService.getUsers.userList()调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM