从某些特定页面导航时刷新标题-Angular 2

时间:2018-11-03 11:01:24

标签: angular

标题-

<nav class="navbar navbar-light">
<div class="container">
    <ul *ngIf="showauthheader == false" class="nav navbar-nav">
      <li class="nav-item">
        <button [routerLink]="['/login']" class="btn btn-light">Login</button>
      </li>
    </ul>
    <ul *ngIf="showauthheader == true" class="nav navbar-nav">
      <li class="nav-item">
        <button [routerLink]="['/']" (click)="logout()" class="btn btn-light">Logout</button>
      </li>
    </ul>
    </div>
</nav>

我正在使用ngOnInit()中设置showauthHeader的值

this.currentUser = JSON.parse(this.userService.getUser())
if (this.currentUser != null)
  this.showauthheader = true
else
  this.showauthheader = false

现在,当我登录系统时,标题没有从Login更改为Logout,因为标题没有刷新。

那么,如何在登录时更改标题?

header.Component.ts

this.userService.isAuthenticated.subscribe(
      (authenticated) => {
        if (authenticated)
          this.showAuthheader = true
        else
          this.showAuthheader = false
      })

service.ts

private isAuthenticatedSubject = new ReplaySubject<boolean>(1);
public isAuthenticated = this.isAuthenticatedSubject.asObservable();

成功登录后,我正在做

this.isAuthenticatedSubject.next(true);

0 个答案:

没有答案