我在html中有以下内容:
<ul class="navbar-nav" >
<li><a *ngIf="isLoggedIn" routerLink="/welcome/in28minutes" class="nav-link">Home</a></li>
<li><a *ngIf="isLoggedIn" routerLink="/todos" class="nav-link">Todos</a></li>
</ul>
问题在于,尽管用户尚未登录,但在页面加载时,这两个<li>
会短暂显示然后消失。
export class MenuComponent implements OnInit {
isLoggedIn = false;
constructor(public hardcodedAuthenticationService : HardcodedAuthenticationService) {}
async ngOnInit() {
await this.isUserLoggedIn()
}
async isUserLoggedIn() {
this.isLoggedIn = await this.hardcodedAuthenticationService.isUserLoggedIn() // this return false
}
}
isLoggedIn最初设置为false。在初始化时,我调用this.hardcodedAuthenticationService.isUserLoggedIn()
,但是它总是返回false,因此isLoggedIn总是false。因此,在html中,不应显示那些2 <LI>
。但是它短暂地显示然后消失了。我不知道为什么。
async isUserLoggedIn() {
let user: any;
if (isPlatformBrowser(this.platformId)) {
user = sessionStorage.getItem('authenticaterUser')
}
if (isPlatformServer(this.platformId)) {
user = this._cookieService.get('authenticaterUser');
}
return !(user === null)
}
答案 0 :(得分:0)
问题是返回!(user === null),返回true。