【问题标题】:Why the condition from localstorage doesn't work?为什么本地存储的条件不起作用?
【发布时间】:2019-03-20 22:26:42
【问题描述】:

在模板组件AppComponent中,变量this.loggedInService.isLoggedIn根据值在logIn()logout()方法之间切换,在应用组件AppComponent中订阅服务@中的这些方法987654326@ 并根据方法,将变量的值更改为 true 或 false。

同样在Guard的方法checkLogin (url: string)中,我根据变量this.loggedInService.isLoggedIn的值返回true或false

一切正常,但是当我重置页面时,我需要保留输入或输出按钮的值。我尝试在服务中的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;
        }
    }
}

【问题讨论】:

  • localStorage.removeItem('login'); 为什么下一行的localStorage.getItem('login') 会返回null 以外的任何内容?它看起来像一个多线程守卫,但我认为它不能在多线程环境中工作。

标签: javascript angular typescript local-storage storage


【解决方案1】:

isLoggedIn 更改为基于 localStorage 项的 get 方法

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(['/']);
  }
}

stackblitz demo

【讨论】:

  • 从模板(html)中删除这个(click)="this.loggedInService.isLoggedIn ? logout() : logIn()"&gt; {{this.loggedInService.isLoggedIn ? 'Exit' : 'Enter'}}
  • @IgorShvets 你更新了 appComponent => this.loggedInService.login() 不是登录和注销正常方法
  • 是的,我这样做了,你能展示你在 StackBlitz 中的方法的例子吗?
  • 如果您根据您的代码创建示例会更好,然后我会更新它
【解决方案2】:

我对你的代码有疑问。

在 LoggedInService 中 onInit 为什么要直接调用 login()logout()

this.CheckAuthentication();
this.login();
this.logout();

这样做是在您的 localStorage 中添加和删除。另外,您可以通过在浏览器控制台中输入localStorage 来检查本地存储中的数据。我认为您应该评论或删除onInit 方法

【讨论】:

  • 我需要保存“guard”和button的值;删除“ngOnit”不会改变任何事情
  • 您可以在 AppComponent html 中调用 this.loggedInService. CheckAuthentication() 或在 AppComponent ngOnInit() 中获取此值一次,而不是引用 this.loggedInService.isLoggedIn 并存储作为 AppComponent 中的 isLoggedIn 变量
猜你喜欢
  • 2016-12-13
  • 1970-01-01
  • 2014-03-21
  • 2020-08-19
  • 2021-01-11
  • 2022-01-11
  • 2021-04-01
  • 1970-01-01
相关资源
最近更新 更多