简体   繁体   中英

Hide the content of app.component file from a single component in Angular

So I am trying to create an Angular app which will ask you to log in before accessing it. I am trying to keep the navbar in the app.component file so it is reused on all the components, but I want to get rid of it on the login page. I thought of just hiding it using CSS, but that might pose a security risk since the navbar will have other routes as well.

I was also thinking of leaving the app.component idea and creat a child navbar component which will be reused in every component, but I'm not sure if that's the best way to do this. Any ideas are greatly appreciated.

I believe you are having login component in router outlet. Update it like

<nav-bar *ngIf="showNavBar"></nav-bar>
<router-outlet (activate)="toggleNavBar($event)"></router-outlet>

in app component

public showNavBar = true;

toggleNavBar(component) {
   if(component instanceof LoginComponent) {
      this.showNavBar = false;
   } else {
      this.showNavBar = true;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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