微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我可以用什么来刷新 observable 而不是 switchMap()?

如何解决我可以用什么来刷新 observable 而不是 switchMap()?

我以 angular 刷新浏览器时注销。 我使用 Authentication 标头处理了我的登录请求和用户请求,因此我确实使用了 observable 和 switchMap 。 现在我的问题是每次使用 switchMap 时,我的 observable 都会刷新,因此用户观察者会发生变化。 我应该怎么做才能获得我的项目列表而不使用 switchMap() ?

 getProjectByToken(): Observable<ProjectList> {
const auth = this.getAuthFromLocalStorage();
if (!auth || !auth.token) {
  return of(undefined);
}
this.isLoadingSubject.next(true);
return this.authHttpService.getTableData(auth.token).pipe(
  map((list: ProjectList) => {
    if (list) {
      console.log(new BehaviorSubject<ProjectList>(list))
      this.currentListSubject = new BehaviorSubject<ProjectList>(list);
    } else {
      console.log(list)
    }
    console.log(list)
    return list;
  }),switchMap(() => this.getLogToken()),finalize(() => this.isLoadingSubject.next(false))
);

}

这是我处理标头请求和设置本地存储的方法

     getProject(){
   console.log(this.currentBtnSubject.asObservable())
   return this.currentListSubject.asObservable()  
  }

我在我的项目组件中使用了这个方法并且我订阅了它

login(email: string,password: string): Observable<usermodel> {
this.isLoadingSubject.next(true);

return this.authHttpService.login(email,password).pipe(
  map((auth: AuthModel) => {
    const result = this.setAuthFromLocalStorage(auth);
    console.log(result)
    return result;
  }),switchMap(() => this.getUserByToken()),switchMap(() => this.getProjectByToken()),catchError((err) => {
    console.error('err',err);
    return of(undefined);
  }),finalize(() => this.isLoadingSubject.next(false))
);

}

在这里,当我使用 switchmap 方法时,没有它,列表根本不会显示

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。