如何在 Angular 6 中优化注销?

问题描述

我有注销功能,对于特定用户,我们需要进行后端 API 调用,然后处理来自 API 调用的响应不需要太多时间,但注销过程最多需要 6-7 秒,我正在寻找方法优化这个,但找不到任何。以下是我的代码

if(localStorage['currentUser']=='xyz'){
          this.http.post('url',email).subscribe(res => {
            localStorage.clear();
            this.router.navigate(['login']).then(() => {
              this.store.dispatch(new logout());
            })
          })
}

非常感谢任何帮助。提前致谢。

解决方法

不要新建Logout(),只需要在函数logout()中使用BehaviorSubject和空值旁边即可。

authenticate$: Subject<User | ErrorMessages> = new BehaviorSubject<
        User | ErrorMessages
    >(null);
...
logout(): void {
    this.authenticate$.next(null);
    this.localStorageService.clearAllCache();
    this.router.navigate(['/login']);
}