【问题标题】:How to call multiple apis call one after another多个api如何调用一个接一个
【发布时间】:2021-09-28 08:48:52
【问题描述】:

我正在学习 RxJS。我有 3 个 api 调用,我需要进行第二个 api 调用,并将其数据作为参数传递给第三个 api 调用。我试过这个:

     checkPermission(permissionName: string): Observable<boolean> {
    this.check(this.p1)
      .pipe(
        switchMap(res => {
          const shouldCheck = res.Value;
          if (shouldCheck.toLowerCase() === 'true') {
            return this.checkPermission(permissionName).pipe(
              map(result => {
               
                return result;
              })
            );
          } else return of(true);
        })
      )
      .subscribe(permission => {
       
      });
   
  }

但出现语法错误。

【问题讨论】:

标签: angular typescript rxjs


【解决方案1】:

您发布的代码不是很容易理解,但我会尝试从中做出一些有用的东西:

  checkPermission(permissionName: string): Observable<boolean> {
    return this.checkSetting(this.p1).pipe(
      map((res) => res.SysConfig.Value.toLowerCase() === 'true'),
      switchMap((shouldCheck) =>
        iif(
          () => shouldCheck,
          this.permission(this.p2).pipe(
            switchMap((data) =>
              this.hasPermission(permissionName, data.SysConfig.Value)
            ),
            // hoping that res.permission is a boolean
            map((res) => res.permission)
          ),
          of(true)
        )
      )
    );
  }

如果您打算返回Observable&lt;boolean&gt;,还应该省略subscribe。你可以在调用的地方订阅这个方法返回的observable:

this.authService.checkPermission(permission).subscribe(hasPermission => {
  console.log(`User ${hasPermission ? 'has' : 'does not have'} ${permission} permission`);
});

【讨论】:

  • 我刚刚更新了工作代码和非工作代码。请检查
  • 我面临一个严重问题,请检查您的聊天记录
【解决方案2】:

您应该以正确的顺序使用switchMap 运算符,即

.pipe(
    switchMap(() => {
        return this.someFunctionThatReturnsObservable();
    }),
    // here you will have access to data you've returned in previous (in data variable)
    switchMap((data) => {
        return this.someOtherRequest(data.param1, data.param2).pipe(
            map(result => {
              return result.permission;
            })
        );
    })
)

请记住,您需要将 rxjs 运算符一个接一个地放置在管道中,并用逗号分隔它们。问题是你有不正确的语法试图将一些没有运算符的代码放在管道内,这些行在这里有问题:

return this.permission(this.p2).pipe(
    const shouldEncrypt = res.SysConfig.Value;

这里不完全清楚你到底想做什么,但我假设你想有条件地返回请求或空 observable,你需要在放置上面的代码之前添加操作符。

【讨论】:

  • 我刚刚更新了工作代码和非工作代码。请检查
【解决方案3】:

试试吧,不过我还没有测试过。

   checkPermission(permissionName: string): Observable<boolean> {
      return this.checkCompanySettingForPermission(this.p1)
          .pipe(
            switchMap(res => this.permissionAPI(res, permissionName)),
            tap(permission => this.$permissionSub.next(permission)),
            switchMap(_=> this.$permissionSub.asObservable())
          )
     }
    
    permissionAPI(res :any, permissionName: string): Observable<any>{
        const shouldCheck = res.SysConfig.Value;
        if (shouldCheck.toLowerCase() === 'true') {
           return this.permissionAPI2(permissionName);
        }
        return of(true);
    }

  permissionAPI2(permissionName: string) :Observable<boolean>{
    return this.checkCompanySettingForPermission(this.p2).pipe(
       switchMap(data => 
        this.hasPermission(permissionName, data.SysConfig.Value).pipe(
              map(( {permission})=>permission)
            )
        )
     );
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多