欢迎各位兄弟 发布技术文章
这里的技术是共享的
Ask QuestionAsked Active 1 year, 7 months agoViewed 375 times1
I have been trying to solve this for 4-5 days, but still can't find the cause of the problem.
As title says, every time I click logout button application browser freezes, can't close the tab, in order to run the app again I have to open another one or close the browser via Task Manager.
I assume the problem is with the ngrx/effect
, I'm using it to clear the localstorage
.
Here is it:
@Effect() logout$: Observable<Action> = this.actions$
.ofType(fromActions.LOGOUT)
.pipe(
tap((action: fromActions.Logout) => {
return this.authService.logout();
}))
Here is the method:
logout() {
localStorage.clear();
}
And also the token is still there, the action is not dispatched, I checked the action, no type errors..
Here are two photos, before logout clicked and after:
EDIT: Action dispatch:
onLogout() {
this.store.dispatch(new fromActions.Logout());
}
Reducer:
case AuthActions.LOGOUT: {
return {
...state,
loading: false,
loggedIn: false
}
javascript angular ngrx ngrx-effectsshareimprove this questionedited Mar 8 '18 at 9:05asked Mar 8 '18 at 8:57merko464
正确答案
In your effect you are returning the Logout action, infinite loop.
Try creating a LogoutSuccess action and return it when Logout succeed.
... .pipe(
tap((action: fromActions.Logout) => {
return this.authService.logout();
}),
map(() => { return {type: 'NO_ACTION'}; })
shareimprove this answeredited Mar 8 '18 at 9:06answered Mar 8 '18 at 9:02ibenjelloun4,180
I tried with return, it's without return, my mistake. – merko Mar 8 '18 at 9:04
The tap
executes the code and returns the passed parameters. You should add a map to return something else then the passed parameter wich is Logout action. I edited my answer to add a code example. – ibenjelloun Mar 8 '18 at 9:07
1Finally, thank you so much :) – merko Mar 8 '18 at 9:11
Could you please provide the reducers code and the dispatch call? – alsami Mar 8 '18 at 9:01
I'm not sure but I think I even don't need reducer code because it's set based on localStorage, if there is a token I set it to
loggedIn
– merko Mar 8 '18 at 9:07