【问题标题】:Angular 2 - forkJoin can't bind the correct lassAngular 2 - forkJoin 无法绑定正确的 lass
【发布时间】:2017-02-21 11:49:47
【问题描述】:

我正在尝试同时进行多个 ajax 调用。 我无法弄清楚这些行有什么问题。好像订阅函数的第二个输入被处理为 Group[] 而不是 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();
            }
        );

接口是:

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

谁能帮我理解问题出在哪里?

【问题讨论】:

  • ([groups,authorizations]) =&gt; 应该做什么?
  • console.log(groups, authorizations)subscribe(...) 回调中添加为第一行时会打印什么?

标签: class angular observable subscribe


【解决方案1】:

试试这样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    相关资源
    最近更新 更多