简体   繁体   English

登录-Angular 8时隐藏可选的导航栏菜单

[英]Hide optional navbar menus when Logged In - Angular 8

I'm trying to hide some of my navbar menus after I logged in. 登录后,我试图隐藏一些导航栏菜单。

Here is my navbar menus 这是我的导航栏菜单

<div class="collapse navbar-collapse" id="navbarSupportedContent">
  <ul class="navbar-nav ml-auto">
    <li class="nav-item active"><a class="nav-link" routerLink="books">Books</a></li> 
  <!--Hide this link-->  <li class="nav-item"><a class="nav-link" routerLink="sign-in">Sign In</a></li> 
  <!--Hide this link-->  <li class="nav-item"><a class="nav-link" routerLink="sign-up">Sign Up</a></li> 
  </ul>
</div>

Is there anyway to hide these commented links that I want to hide? 无论如何,有什么地方可以隐藏我要隐藏的评论链接? here is my SignIn Component Type Script 这是我的登录组件类型脚本

export class SignInComponent implements OnInit {

  email: string;
  password: string;

  constructor(public auth: AuthService) { }

  ngOnInit() {
  }

  login(){
    this.auth.login(this.email, this.password);
    this.email = this.password = '';
  }

}

Auth Service Type Script 验证服务类型脚本

  login(email: string, password: string){
    this.afAuth.auth.signInWithEmailAndPassword(email, password).then(
      value => {
        console.log('Success!', value);
        this.router.navigate(['dashboard']);
      }
    ).catch(err=>{
      console.log('Something went wrong:',err.message);
      this.router.navigate(['sign-up']);
    })
  }

** If this will help, here is my Header Type Script **如果这有帮助,这是我的标题类型脚本

export class HeaderComponent implements OnInit {

  title = 'Book List App';

  constructor() { }

  ngOnInit() {
  }

}

Thank you. 谢谢。

Return a boolean value true/false based on the success, 根据成功返回布尔值true / false,

boolean login(email: string, password: string){
    this.afAuth.auth.signInWithEmailAndPassword(email, password).then(
      value => {
        console.log('Success!', value);
        this.router.navigate(['dashboard']);
        return true;
      }
    ).catch(err=>{
      console.log('Something went wrong:',err.message);
      this.router.navigate(['sign-up']);
      return false;
    })
  }

and assign to a variable in your component as, 并分配给您组件中的变量,

this.loggedIn =   this.auth.login(this.email, this.password);;

bind the variable in your HTML 在HTML中绑定变量

<li class="nav-item" *ngIf="loggedIn"><a class="nav-link" routerLink="sign-in">Sign In</a></li> 
<li class="nav-item"  *ngIf="loggedIn"><a class="nav-link" routerLink="sign-up">Sign Up</a></li> 

You can save to session storage state of login or not. 您可以将登录状态保存为会话存储状态,也可以不保存。

login(email: string, password: string){
        this.afAuth.auth.signInWithEmailAndPassword(email, password).then(
          value => {
            sessionStorage.setItem("loggedIn", email);
            console.log('Success!', value);
            this.router.navigate(['dashboard']);
          }
        ).catch(err=>{
          console.log('Something went wrong:',err.message);
          this.router.navigate(['sign-up']);
        })
      }

And add a variable flag to check logged in or not 并添加变量标志以检查是否已登录

export class HeaderComponent implements OnInit {

  title = 'Book List App';

  isLoggedIn : boolean;
  constructor() { }

  ngOnInit() {
      isLoggedIn = sessionStorage.getItem("loggedIn") != undefined;
  }

}

Update html 更新HTML

<div class="collapse navbar-collapse" id="navbarSupportedContent">
  <ul class="navbar-nav ml-auto">
    <li class="nav-item active"><a class="nav-link" routerLink="books">Books</a></li> 
  <!--Hide this link-->  <li *ngIf="isLoggedIn" class="nav-item"><a class="nav-link" routerLink="sign-in">Sign In</a></li> 
  <!--Hide this link-->  <li *ngIf="isLoggedIn" class="nav-item"><a class="nav-link" routerLink="sign-up">Sign Up</a></li> 
  </ul>
</div>

You are using angular firebase, so you can listen to the authState , which returns you an observable of firebase.User when logged in or null when not logged in. It emits every time when the auth state changes. 您正在使用的角度火力点,这样你就可以听authState ,这将返回一个可观察的firebase.User或登录时, null未登录时,它发出每当AUTH状态更改时间。 You can use the auth state in a service, store it in a variable so that it is usable all over the app. 您可以在服务中使用auth状态,将其存储在变量中,以便可以在整个应用程序中使用。 Components then subscribe to the observable. 然后组件订阅可观察对象。

Service: 服务:

import { User } from 'firebase';

// ...

user$: Observable<User>;

constructor(...) {
   this.user$ = this.afAuth.authState;
}

Component: 零件:

import { User } from 'firebase';
import { AuthService } from '....'

// ...

user$: Observable<User>;

contructor(private authService: AuthService) { 
  this.user$ = this.authService.user$;
}

html: HTML:

<div *ngIf="!user$ | async">...</div> 
<div *ngIf="user$ | async">...</div>

You can also use *ngIf ... else if you don't want two *ngIf 's. 您也可以使用*ngIf ... else如果您不想使用两个*ngIf

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

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