如何刷新后如何使页面与signOut一起使用

时间:2018-02-20 09:41:29

标签: javascript angular

我有主页,其中我有登录和注销栏。 我有用户页面,其中还有一个signUp表单。 现在,如果我签名,它工作正常,但一旦我刷新页面,它仍然工作,因为用户不是signOut。 任何人都可以帮我解决我的问题。

用户页面的HTML:

<ul>
<li class="signUp" *ngIf = "loggedIn">{{userName}}</li>
<li class="signIn" (click)="signOut()" *ngIf = "loggedIn">Sign Out</li>
<li class="signUp" (click)="openSignUp(user)"  *ngIf = "!loggedIn">Sign Up</li>
<li class="signIn" (click)="openSignIn(logedin)"  *ngIf = "!loggedIn">Sign In</li>
</ul>

TS:

**In service:** 



 private signOutSource = new Subject<any>(); 
    signoutDone$ = this.signOutSource.asObservable(); 

signOut() { 
this.signOutSource.next(); 
} 

home.ts:

export class HomeComponent implements OnDestroy { 
constructor(private ApiService:ApiService) { 
this.subscription = ApiService.signinDone$.subscribe( 
user => { 
this.userName = user.name; 
localStorage.setItem('loggedin', 'true'); 
localStorage.setItem('user', JSON.stringify(user)); 
this.loggedIn = true; 
this.isLoadingSignUp = false; 
this.ApiService.getUserData(user); 
}); 
if(localStorage.getItem('loggedin')){ 
this.loggedIn = true; 
} else { 
this.loggedIn = false; 
} 
if(localStorage.getItem('user')){ 
this.userName = JSON.parse(localStorage.getItem('user')).name; 
} 
this.logoutSubscription = ApiService.signoutDone$.subscribe( 
user => { 
localStorage.setItem('loggedin', 'false'); 
this.loggedIn = false; 
this.ApiService.getUserData({}); 
}); 
} 

ngOnDestroy() { 
this.subscription.unsubscribe(); 
} 

signOut() { 
this.ApiService 
.logout() 
.subscribe( 
user => { 
localStorage.removeItem('user'); 
localStorage.removeItem('loggedin'); 
this.ApiService.getUserData({}); 
this.loggedIn = false; 
this.router.navigate(['/home']); 
this.toasterService.pop('success', 'SignOut Successfull'); 
this.ApiService.signOut(); 
},error => { 
if(error.data && error.data.length > 0) { 
this.toasterService.pop('error', error.data); 
} else { 
this.toasterService.pop('error', 'Something went wrong!'); 
} 
}); 
} 

} 

User.componet:

export class UserComponent implements OnDestroy { 
constructor(private ApiService:ApiService) { 
this.subscription = ApiService.signinDone$.subscribe( 
user => { 
this.userName = user.name; 
localStorage.setItem('loggedin', 'true'); 
localStorage.setItem('user', JSON.stringify(user)); 
this.loggedIn = true; 
this.isLoadingSignUp = false; 
this.ApiService.getUserData(user); 
}); 
if(localStorage.getItem('loggedin')){ 
this.loggedIn = true; 
} else { 
this.loggedIn = false; 
} 
if(localStorage.getItem('user')){ 
this.userName = JSON.parse(localStorage.getItem('user')).name; 
} 
this.logoutSubscription = ApiService.signoutDone$.subscribe( 
user => { 
localStorage.setItem('loggedin', 'false'); 
this.loggedIn = false; 
this.ApiService.getUserData({}); 
}); 
} 

ngOnDestroy() { 
this.subscription.unsubscribe(); 
} 

}

用户页面的HTML:

 <div class="favourate" *ngIf="!loggedIn">
    <h1>User</h1>
    <hr />
    <div class="backImage">
      <form name="signUpForm" class="signUpForm" #signUpForm="ngForm" novalidate>
        <div class="form-group">
          <h3>Sign Up</h3>
        </div>
        <div class="form-group input">
          <mat-form-field> 
            <mat-icon>perm_identity</mat-icon>
            <input matInput type="text" placeholder="Name" name="name" [(ngModel)]="name" #Name="ngModel" required>
          </mat-form-field>
        </div>
        <div class="form-group input">
          <mat-form-field> 
            <mat-icon>email</mat-icon>
            <input matInput type="email" placeholder="Email" name="email" [(ngModel)]="user.email" #Email="ngModel" pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$" required>
          </mat-form-field>
        </div>
        <div class="form-group input">
          <mat-form-field> 
            <mat-icon>lock</mat-icon>
            <input matInput type="password" placeholder="Password" name="password" [(ngModel)]="user.password" #Password="ngModel" required>
          </mat-form-field>
        </div>          
      </form>
    </div>
  </div>

1 个答案:

答案 0 :(得分:1)

由于您要在loggedIn中存储local storage个详细信息, 只需添加localstorage签入ngOnOnit

export class AppComponent implements OnInit, OnDestroy { 

    ngOnInit() { 
        if(localStorage.getItem('loggedin')){ 
            if(localStorage.getItem('loggedin') == 'true') 
            { 
                this.loggedIn = true; 
            } 
            else
            { 
                this.loggedIn = false; 
            } 
        }else{
            this.loggedIn = false; 
        }
    }
}