0

我发现了几个与我类似的问题,但大部分与参数有关,答案并没有帮助我解决我的问题。基本上问题如下:一旦用户登录,用户就可以在导航栏中看到他/她的用户名,当他们点击它时,页面将用户带到个人资料页面,第一次完美运行,当用户注销并使用不同的用户名登录时,除非刷新页面,否则旧用户的信息仍然存在。

服务帐号

getUserInformation(userId: number) {
if (!this.userInformation$) {
  this.userInformation$ = this.http.post<any>(this.baseUrlGetUserInfo, { userId }).pipe(shareReplay());
}
return this.userInformation$;
}

在初始化方法上

ngOnInit() {
this.route.params.subscribe(params => {
  this.loaderSpinner = true;
  // tslint:disable-next-line: radix
  // this.userId = this.accntService.currentUserId;
  // tslint:disable-next-line: radix
  this.accntService.currentUserId.subscribe(res => {
    this.userId = res;
  });
  // tslint:disable-next-line: radix
  this.userInformation$ = this.accntService.getUserInformation(parseInt(this.userId));
  this.userInformation$.subscribe(result => {
    this.userInformation = result.userInformation;
    console.log(this.userInformation);
    this.loaderSpinner = false;
  }, error => {
    this.loaderSpinner = false;
    console.error(error);
  });
});
}

导航栏 HTML

<a [routerLink]="['/profile']" class="nav-link" *ngIf="(userName$ | async) as userName">
          {{userName}}
        </a>

有人可以帮我吗?

提前致谢。

4

1 回答 1

0

这就是问题所在,我发现了问题。基本上错误出现在 onDestroy 中。

private getUserInfo(): void {
this.loaderSpinner = true;
this.accntService.currentUserId.pipe(takeUntil(this.$ondestroy)).subscribe(res => {
  this.userId = res;
});

this.accntService.getUserInformation(this.userId).pipe(takeUntil(this.$ondestroy)).subscribe(response => {
  this.userInformation = response.userInformation;
  this.loaderSpinner = false;
}, () => {
  this.loaderSpinner = false;
});
}

ngOnInit() {
this.getUserInfo(); }

ngOnDestroy() {
this.$ondestroy.next(true);
}
于 2020-01-17T14:09:08.740 回答