简体   繁体   English

Angular2路由器

[英]Angular2 router

I have been creating a web application with Angular2 and Firebase, first I check if the user is authenticated or not, if user is authenticated, he will be redirect to a page that contains the list of groups otherwise he will be redirect to a login page. 我一直在使用Angular2和Firebase创建一个Web应用程序,首先我检查用户是否通过了身份验证,如果用户通过了身份验证,他将被重定向到包含组列表的页面,否则他将被重定向到登录页面。 。
The problem is that the menu and the header are always visible. 问题在于菜单和标题始终可见。 I want the menu and the header of the application will not be displayed when user redirect to login page. 我希望当用户重定向到登录页面时,菜单和应用程序的标题不会显示。 how I can do this. 我该怎么做。

app.component.ts app.component.ts

  constructor(public af: AngularFire,private  router:Router){
      this.af.auth.subscribe(auth => {
     if(auth) {
       this.userconnected = auth;
     }
}

app.component.html 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 app.routes.ts

  { path: 'groups', component: ListgroupsComponent, canActivate: [AuthGuard] },   
  { path: '', redirectTo: 'groups', pathMatch: 'full' },
  { path: 'login', component: LoginComponent },

groups.component.ts 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;
  }

Thanks in advance. 提前致谢。

Your approach is wrong. 您的方法是错误的。 You are using router-outlet in your app.component.html with menu and header code that's why the menu and the header of the app will always be displayed when user redirects to login page. 您正在app.component.html中使用带有菜单和标题代码的router-outlet ,这就是为什么当用户重定向到登录页面时始终显示应用程序的菜单和标题的原因。 The problem is in your rendering because your login page is rendered inside your header and menu div code. 问题出在您的渲染中,因为您的登录页面是在标题和菜单div代码内渲染的。

There could be many ways but according to me, your app should firstly show login page If the login is successful then It should redirect to some main page in which you will have header and menu as in your app.component.html. 可能有很多方法,但是根据我的看法,您的应用程序应首先显示登录页面。如果登录成功,则应将其重定向到某个主页面,您将在其中具有标题和菜单,如app.component.html中所示。

The issue is that you need to change your this.userconnected state on logout. 问题是您需要在注销时更改this.userconnected状态。

app.component.ts will check and set that value only inside of the constructor . app.component.ts将仅在constructor内部检查和设置该值。

Try to subscribe to onAuthStateChanged and set there your this.userconnected state. 尝试订阅onAuthStateChanged并在onAuthStateChanged设置this.userconnected状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM