如何在 Angular 6 中优化注销?

Posted

技术标签:

【中文标题】如何在 Angular 6 中优化注销?【英文标题】:How to optimize logout in Angular 6? 【发布时间】:2021-04-17 00:20:42 【问题描述】:

我有注销功能,对于特定用户,我们需要进行后端 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());
            )
          )

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

【问题讨论】:

【参考方案1】:

不要新建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']);

【讨论】:

以上是关于如何在 Angular 6 中优化注销?的主要内容,如果未能解决你的问题,请参考以下文章