0

rxjs 5.5.12中,我创建了一个具有多个 observable的login()tosubscribedo()

login(userEmail, userPassword).subscribe((response) => {
  console.log(response);

  if (response === 'something') {
    // some actions
  } else {
    // some actions
  }
},(error) => {
  // some error actions
});

我使用 rxLoginPOST 调用 api:

rxLoginPOST(url, params) {
  return Rx.Observable.fromPromise(somePromise).catch(handleErrFunction);
}

我在登录函数中使用它,它返回多个 observable:

login(email, password) {
  return APIService.shared.rxLoginPOST('users/signin', { ...arguments })
  .flatMap((response) => {
    if (response.status === 200) {
        return Rx.Observable.combineLatest(
          myObservable1,
          myObservable2,
          myObservable3
        )
      } else {
        return Rx.Observable.of(response)
      }
  }).do((result) => {
      if (result.length > 0) {
        // some logic
      } else {
        return Rx.Observable.of(result)
      }
  }).flatMap((result) => {
      if (result.length > 0) {
        return this.checkUserIsRegistered()
      } else {
        return Rx.Observable.of(result)
      }
  })
}

rxjs 6.5.3,我改变了所有的导入,比如

import { Observable, combineLatest, of } from 'rxjs';
import { mergeMap, map, catchError, flatMap, tap } from 'rxjs/operators';

如果我触发login()它会显示do is not a function

所以我改变了代码:

login(userEmail, password) {
    return APIService.shared.rxLoginPOST('users/signin', { ...arguments }).pipe(
        mergeMap((response) => {
        if (response.status === 200) {
          return combineLatest(
            myObservable1,
            myObservable2,
            myObservable3
          )
        } else {
          return of(response)
        }
      }).tap((result) => { // I am stuck here, I have no idea how to continue with it...
         console.log('check 4');  // check 4 does not appear.
         console.log('response', response);
         return of(response)
      }
    );

我尝试使用tap而不是do,但检查 4 没有出现。

任何帮助,将不胜感激。

4

1 回答 1

1

使用pipe,您应该像这样链接您的运营商:pipe(op1, op2)而不是喜欢pipe(op1).op2()

login(userEmail, password) {
    return APIService.shared.rxLoginPOST('users/signin', { ...arguments })
      .pipe(
          mergeMap((response) => {
            if (response.status === 200) {
              return combineLatest(
                myObservable1,
                myObservable2,
                myObservable3
              )
            }
            return of(response)
          }),
          tap((result) => { 
            console.log('check 4');  // check 4 does not appear.
            console.log('response', response);
          })
        );

我还删除了您无用的else声明,因为您之前返回了一些东西:)

编辑:正如评论中所建议的,return来自的指令tap也已被删除,因为不必要

于 2019-10-30T09:45:41.610 回答