2017-02-21 67 views
1

我正在嘗試一起執行多個Ajax調用。 我找不出這些線路有什麼問題。好像訂閱功能的第二輸入作爲處理組[],而不是授權[]Angular 2 - forkJoin不能綁定正確的黑客

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

接口包括:

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

任何人都可以幫助我瞭解什麼是問題呢?

+0

什麼是'([團體,授權)=>'該怎麼辦? –

+0

當''subscribe(...)'回調中的第一行添加'console.log(groups,authorizations)'時會打印什麼? –

回答

0

試試這樣說:

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

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