Angular 2 - forkJoin can't bind the correct lass

久未见 提交于 2019-12-11 05:55:51

问题


I'm trying to do multiple ajax calls together. I can't figure out what is wrong with these lines. Seems like the second input of the subscribe function is processed as Group[] instead of Authorization[]

 Observable.forkJoin(
            [this.userService.getAllGroups(),this.userService.getAllAuthorizations()]
        )
        .subscribe(
            ([groups,authorizations]) => {
                this.groups = groups;
                this.authorizations = authorizations; //error: Type 'Group[]' is not assignable to type 'Authorization[]'
                this.loaderService.hideLoader();
            },
            (err)=>{
                this.loaderService.hideLoader();
            }
        );

Interfaces are:

(method) UserService.getAllGroups(): Observable<Group[]>
(method) UserService.getAllAuthorizations(): Observable<Authorization[]>

Anyone can help me understand what the is problem?


回答1:


Try it like this:

Observable.forkJoin<Group[], Authorization[]>(
         this.userService.getAllGroups(),
         this.userService.getAllAuthorizations()
      ).subscribe(results => {
         this.groups = results[0];
         this.authorizations = results[1];
      );

or

Observable.forkJoin<Group[], Authorization[]>(
         this.userService.getAllGroups(),
         this.userService.getAllAuthorizations()
      ).subscribe((groups, authorizations) => {
         this.groups = groups;
         this.authorizations = authorizations;
      );


来源:https://stackoverflow.com/questions/42366511/angular-2-forkjoin-cant-bind-the-correct-lass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!