小编Ale*_*lex的帖子

订阅调用后的空数组

我写了一个服务:

loadroom() : Observable<any[]>{
    return this.http.get(this.roomUrl)
        .pipe(
            map((response: any) => {
                let resources = response;
                return resources.map(function(room:any) {
                    return {id: room.id, title: room.title, eventcolor: room.eventColor};
                });
            })
        );
}
Run Code Online (Sandbox Code Playgroud)

然后我在一个组件中使用这个服务

rooms: any[] = [];       
ngOnInit() {
    this.schedulerService.loadroom().subscribe(response => this.rooms = response);
    console.log(this.rooms);
}
Run Code Online (Sandbox Code Playgroud)

我没有看到数组中的数据。在控制台中,我得到一个空数组 []

但如果我使用

this.schedulerService.loadroom().subscribe(response => {console.log(response)});
Run Code Online (Sandbox Code Playgroud)

我得到数据。

angular

0
推荐指数
1
解决办法
642
查看次数

标签 统计

angular ×1