login$ = createEffect(() => {
return this.action$.pipe(
ofType(loginStart),
exhaustMap(action => {
return this.authService
.login({
username: action.username,
password: action.password,
})
.pipe(
map(data => {
const user = this.authService.handleLogin(data);
return loginSuccess(user);
}),
catchError(error => {
return of(loginFailure({ error: error.message }));
})
);
})
);
});
在上面的代码片段中,返回
loginSuccess
减速器成功运行,但对减速器 loginSuccess
的相同调用不适用于以下函数:
autoAuthenticate$ = createEffect(() => {
return this.action$.pipe(
ofType(autoAuthenticate),
map(() => {
const user = this.authService.getAuthDataFromLocalStorage();
if (user && user._expiration && user._expiration > new Date()) {
this.authService.onSuccessfulAuthentication(user._expiration);
return loginSuccess(user);
} else {
return loginFailure({ error: 'Login Failed' });
}
})
);
});
函数正在成功执行,因为还有一个效果已被两个片段成功执行。
loginRedirect$ = createEffect(
() => {
return this.action$.pipe(
ofType(loginSuccess),
map(() => {
this.router.navigate(['home']);
})
);
},
{ dispatch: false }
);
}
我无法弄清楚为什么在第二种情况下没有调用减速器,因为在这两种情况下都调度了操作。
第二种情况下代码是否达到了调度
loginSuccess(user)
?