在rxjs 5.5.12
中,我创建了一个具有多个 observable的login()
tosubscribe
do()
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 没有出现。
任何帮助,将不胜感激。