问题
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