我的导航栏在我的 app.component.html 中:
<nav class="navbar navbar-expand-lg navbar-light bg-light" *ngIf="toShow">
<a class="navbar-brand" href="#">
<img src="assets/leitwind_logo.png" alt="logo">
</a>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" routerLink="/app-dashboard">Overview <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" routerLink="/app-events">EventLog</a>
<li class="nav-item">
<a class="nav-link" href="#" (click)="Logout()">Exit </a>
</li>
</ul>
</div>
</nav>
<router-outlet></router-outlet>
在我的 app.component.ts 中:
token: string;
toShow:boolean=false;
ngOnInit(){
if (sessionStorage.getItem('token')){
this.toShow=true;
}
else
{
this.toShow=false;
}
}
Logout(){
sessionStorage.removeItem("token");
}
当我登录时,所有内容都显示出来,但 NavBar,即使我在那里设置了条件,知道吗?