为什么角度ngx效果如果失败一次就不起作用?



我使用角度6和Ngrx效果。 这是一种登录效果

@Effect({dispatch: false})
login$ = this.actions$.pipe(
ofType<Login>(AuthActionTypes.Login),
tap(action => {
localStorage.setItem(environment.authTokenKey, action.payload.authToken);
console.log('login effect');
this.store.dispatch(new UserRequested());
}),
);

它调度用户请求效果

@Effect({dispatch: false})
loadUser$ = this.actions$
.pipe(
ofType<UserRequested>(AuthActionTypes.UserRequested),
withLatestFrom(this.store.pipe(select(isUserLoaded))),
filter(([action, _isUserLoaded]) => !_isUserLoaded),
mergeMap(([action, _isUserLoaded]) => this.auth.getUserByToken()),
tap(data => {
console.log('login effect');
if (data) {
this.store.dispatch(new UserLoaded({ user: data['user'] }));
localStorage.setItem('options', JSON.stringify(data['options']));
// localStorage.setItem("permissions", data['user'].permissions_list);
data['user'].permissions_list.forEach((item) => {
this.permissionsService.addPermission(item.name);
});
} else {
this.store.dispatch(new Logout());
}
}, error => {
this.store.dispatch(new Logout());
})
);

如果此效果被调用并且至少失败一次,则不会再次调用它。为什么?

因为需要控制流。如果流出现错误,它将按预期停止。

如果您希望它不停止,请考虑将catchError运算符与throwError函数一起使用,或者只是在订阅中捕获错误。

现场观看 :

不工作

rxjs.throwError('mocked error')
.subscribe(
() => console.log('You should not see this message'),
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.3/rxjs.umd.js"></script>

加工

rxjs.throwError('mocked error')
.subscribe(
() => console.log('You should not see this message'),
() => console.log('You should see this message')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.3/rxjs.umd.js"></script>

工作中的国际清算银行

rxjs.throwError('mocked error')
.pipe(rxjs.operators.catchError(err => rxjs.of('some mocked replacement value')))
.subscribe(
() => console.log('You should see this message'),
() => console.log('You should not see this message')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.3/rxjs.umd.js"></script>

最新更新