Javascript NgIf在angular 2的模板中无法正常工作
我有以下angular 2的测试代码,其中我根据本地存储中是否有令牌输出true或false。在这种情况下,我确实有它,因为我可以在我的chrome开发者工具的应用程序下看到它。然而,我甚至认为我看到了 isLoggedIn()返回true 多次渲染此组件时,我还看到浏览器显示…false。。。在页面上。有人知道为什么吗Javascript NgIf在angular 2的模板中无法正常工作,javascript,angular,Javascript,Angular,我有以下angular 2的测试代码,其中我根据本地存储中是否有令牌输出true或false。在这种情况下,我确实有它,因为我可以在我的chrome开发者工具的应用程序下看到它。然而,我甚至认为我看到了 isLoggedIn()返回true 多次渲染此组件时,我还看到浏览器显示…false。。。在页面上。有人知道为什么吗 import { Component } from '@angular/core'; @Component({ selector: 'app-authenticati
import { Component } from '@angular/core';
@Component({
selector: 'app-authentication',
template: `
<div *ngIf="isLoggedIn()">
...true...
</div>
<div *ngIf="!isLoggedIn()">
...false...
</div>
`
})
export class AuthenticationComponent {
isLoggedIn() {
console.log('isLoggedIn() returns', localStorage.getItem('token') !== null)
return (localStorage.getItem('token') !== null);
}
}
从'@angular/core'导入{Component};
@组成部分({
选择器:“应用程序身份验证”,
模板:`
…真的。。。
…错。。。
`
})
导出类AuthenticationComponent{
伊斯洛格丁(){
console.log('isLoggedIn()返回',localStorage.getItem('token')!==null)
返回(localStorage.getItem('token')!==null);
}
}
您确实在return(localStorage.getItem('token')!==null)设置了断点,并真正验证了返回值是否为真,对吗?您是否在代码中的任何地方更改了“token”?您确实在return(localStorage.getItem('token')!==null)设置了断点
并真正验证了返回值是否为真,对吗?你会在代码中的任何地方更改“token”吗?