为什么本地存储的条件不起作用?
Why the condition from localstorage doesn't work?
在模板组件AppComponent
中,根据值,变量this.loggedInService.isLoggedIn
在logIn()
和logout()
方法之间切换,这在应用程序组件AppComponent
在服务 LoggedinService
中订阅了这些方法,并根据方法将变量的值更改为 true 或 false。
同样在Guard的方法中checkLogin (url: string)
我return真假取决于变量的值this.loggedInService.isLoggedIn
一切正常,但是当我重置页面时,我需要保留输入或输出按钮的值。我尝试在服务中的 login()
和 logout()
方法中执行此操作,但重新加载页面后,更改仍未保存。帮忙解决这个问题,让页面重启后更改仍然存在。
AppComponent 模板:
<li class="nav-item">
<a class="btn btn-outline-success"
[class.btn-outline-success]="!this.loggedInService.isLoggedIn"
[class.btn-outline-danger]="this.loggedInService.isLoggedIn"
(click)="this.loggedInService.isLoggedIn ? logout() : logIn()">
{{this.loggedInService.isLoggedIn ? 'Exit' : 'Enter'}}
</a>
</li>
AppComponent代码:
export class AppComponent implements OnInit {
constructor(private loggedInService: LoggedinService,
private router: Router) {
}
ngOnInit() {}
logIn(): void {
this.loggedInService.login();
if (this.loggedInService.isLoggedIn) {
let redirect = this.loggedInService.redirectUrl ? this.loggedInService.redirectUrl :
'/gallery';
this.router.navigate([redirect]);
}
}
logout(): void {
this.loggedInService.logout();
this.router.navigate(['/']);
}
}
登录服务:
export class LoggedinService implements OnInit {
isLoggedIn: boolean = false;
redirectUrl: string;
constructor() {
}
ngOnInit() {
this.CheckAuthentication();
}
enter code here
CheckAuthentication(): boolean {
if (localStorage.getItem('login') === 'true') {
return this.isLoggedIn = true;
} else if (localStorage.getItem('login') === 'false') {
return this.isLoggedIn = false;
}
}
login() {
localStorage.setItem('login', 'true')
}
logout() {
localStorage.removeItem('login');
localStorage.setItem('login', 'false')
}
AuthGuard:
export class AuthGuard implements CanActivate {
constructor(private loggedInService: LoggedinService) {
}
canActivate(next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean{
let url: string = state.url;
return this.checkLogin(url);
}
checkLogin(url: string): boolean {
if (this.loggedInService.isLoggedIn) {
return true;
} else {
this.loggedInService.redirectUrl = url;
return false;
}
}
}
我对你的代码有疑问。
在 LoggedInService onInit
为什么要直接调用 login()
和 logout()
?
this.CheckAuthentication();
this.login();
this.logout();
这样做是在您的 localStorage 中添加和删除。此外,您可以通过在浏览器中输入 localStorage
来检查本地存储中的数据 console.I 认为您应该评论或删除 onInit
方法
将 isLoggedIn
更改为 get
基于 localStorage 项的方法
export class LoggedinService implements OnInit {
redirectUrl: string;
constructor() {}
get isLoggedIn(): boolean {
return localStorage.getItem('login') ? true : false;
}
login(){
localStorage.setItem('login','true')
}
logout(){
localStorage.removeItem('login')
}
}
app.component
export class AppComponent {
constructor(private loggedInService: LoggedinService,
private router: Router) {
}
logIn(): void {
this.loggedInService.login(); // set the state as login
let redirect = this.loggedInService.redirectUrl ? this.loggedInService.redirectUrl :
'/gallery';
this.router.navigate([redirect]);
}
logout(): void {
this.loggedInService.logout(); //// set the state as logout
this.router.navigate(['/']);
}
}
在模板组件AppComponent
中,根据值,变量this.loggedInService.isLoggedIn
在logIn()
和logout()
方法之间切换,这在应用程序组件AppComponent
在服务 LoggedinService
中订阅了这些方法,并根据方法将变量的值更改为 true 或 false。
同样在Guard的方法中checkLogin (url: string)
我return真假取决于变量的值this.loggedInService.isLoggedIn
一切正常,但是当我重置页面时,我需要保留输入或输出按钮的值。我尝试在服务中的 login()
和 logout()
方法中执行此操作,但重新加载页面后,更改仍未保存。帮忙解决这个问题,让页面重启后更改仍然存在。
AppComponent 模板:
<li class="nav-item">
<a class="btn btn-outline-success"
[class.btn-outline-success]="!this.loggedInService.isLoggedIn"
[class.btn-outline-danger]="this.loggedInService.isLoggedIn"
(click)="this.loggedInService.isLoggedIn ? logout() : logIn()">
{{this.loggedInService.isLoggedIn ? 'Exit' : 'Enter'}}
</a>
</li>
AppComponent代码:
export class AppComponent implements OnInit {
constructor(private loggedInService: LoggedinService,
private router: Router) {
}
ngOnInit() {}
logIn(): void {
this.loggedInService.login();
if (this.loggedInService.isLoggedIn) {
let redirect = this.loggedInService.redirectUrl ? this.loggedInService.redirectUrl :
'/gallery';
this.router.navigate([redirect]);
}
}
logout(): void {
this.loggedInService.logout();
this.router.navigate(['/']);
}
}
登录服务:
export class LoggedinService implements OnInit {
isLoggedIn: boolean = false;
redirectUrl: string;
constructor() {
}
ngOnInit() {
this.CheckAuthentication();
}
enter code here
CheckAuthentication(): boolean {
if (localStorage.getItem('login') === 'true') {
return this.isLoggedIn = true;
} else if (localStorage.getItem('login') === 'false') {
return this.isLoggedIn = false;
}
}
login() {
localStorage.setItem('login', 'true')
}
logout() {
localStorage.removeItem('login');
localStorage.setItem('login', 'false')
}
AuthGuard:
export class AuthGuard implements CanActivate {
constructor(private loggedInService: LoggedinService) {
}
canActivate(next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean{
let url: string = state.url;
return this.checkLogin(url);
}
checkLogin(url: string): boolean {
if (this.loggedInService.isLoggedIn) {
return true;
} else {
this.loggedInService.redirectUrl = url;
return false;
}
}
}
我对你的代码有疑问。
在 LoggedInService onInit
为什么要直接调用 login()
和 logout()
?
this.CheckAuthentication();
this.login();
this.logout();
这样做是在您的 localStorage 中添加和删除。此外,您可以通过在浏览器中输入 localStorage
来检查本地存储中的数据 console.I 认为您应该评论或删除 onInit
方法
将 isLoggedIn
更改为 get
基于 localStorage 项的方法
export class LoggedinService implements OnInit {
redirectUrl: string;
constructor() {}
get isLoggedIn(): boolean {
return localStorage.getItem('login') ? true : false;
}
login(){
localStorage.setItem('login','true')
}
logout(){
localStorage.removeItem('login')
}
}
app.component
export class AppComponent {
constructor(private loggedInService: LoggedinService,
private router: Router) {
}
logIn(): void {
this.loggedInService.login(); // set the state as login
let redirect = this.loggedInService.redirectUrl ? this.loggedInService.redirectUrl :
'/gallery';
this.router.navigate([redirect]);
}
logout(): void {
this.loggedInService.logout(); //// set the state as logout
this.router.navigate(['/']);
}
}