Angular2とFirebaseでWebアプリケーションを作成しました。 最初にユーザーが認証されているかどうかを確認します。ユーザーが認証されていれば、グループの一覧を含むページにリダイレクトされます。ログインページにリダイレクトされます。
メニューとヘッダーが常に表示されるという問題があります。 ログインページにリダイレクトすると、メニューが表示され、アプリケーションのヘッダーは表示されません。 私はこれをどのようにすることができます。Angular2ルーター
app.component.ts
constructor(public af: AngularFire,private router:Router){
this.af.auth.subscribe(auth => {
if(auth) {
this.userconnected = auth;
}
}
app.component.html
<app-appheader *ngIf="userconnected" [userconnected]="userconnected"></app-appheader>
<app-appmenu *ngIf="userconnected" [userconnected]="userconnected" data-spy="affix"></app-appmenu>
<div class="content-wrapper">
<section class="content">
<div class="row">
<router-outlet></router-outlet>
</div>
</section>
</div>
<app-appfooter></app-appfooter>
app.routes.ts
{ path: 'groups', component: ListgroupsComponent, canActivate: [AuthGuard] },
{ path: '', redirectTo: 'groups', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
groups.component.ts
ngOnInit() {
console.log("ngOnInit list surveu");
this.af.auth.subscribe(auth => {
if(auth) {
this.name = auth;
this.ifInfoUser(this.name.auth.uid)
}
});
}
ifInfoUser(idUser):boolean{
console.log('BeginifInfoUser');
const user = this.af.database.object('/users/'+idUser);
user.subscribe(data => {
console.log("data");
if(data.uid!=null){
this.existUser=true;
this.router.navigate(['/groups']);
}else{
this.existUser=false;
this.router.navigate(['/login']);
}
});
return this.existUser;
}
ありがとうございます。
ユーザーが認証されているかどうかを確認する[guard](https://blog.thoughtram.io/angular/2016/07/18/guards-in-angular-2.html)を使用します。受け取った情報に基づいて何をすべきかを決定します。 – Adam
私はすでにユーザーが接続されているかどうかを確認しています "* ngIf =" userconnected "" – naruto
問題はなんですか?あなたの現在のアプローチはあなたのために働いていませんか?メニューは常に表示されますか?もしそうなら、ロジックエラーが発生している可能性があります。 –