angular 2 - how to hide nav bar in some components

后端 未结 9 736
半阙折子戏
半阙折子戏 2020-12-04 16:07

I am created nav bar separately in nav.component.html ,how to hide nav bar in some components like login.component.

nav.component.html

9条回答
  •  情书的邮戳
    2020-12-04 16:43

    I was able to solve this without using a nav/toolbar service by adding a data object to the route in the route.module. I expanded on Todd Motto's example of adding dynamic titles to a page and added toolbar: false/true to the data object in my path. I then subscribed to the router events in my toolbar.component. Using Todd's event listener func, I read the path object and used the boolean value to set the toolbar visible or not visible.

    No service needed and works on pagerefresh.

    routing module

    ...
    const routes: Routes = [
    { path: 'welcome', component: WelcomeComponent, data: { title: 'welcome', toolbar: false} }, ...];
    

    toolbar.component

    constructor(private router: Router, private activatedRoute: ActivatedRoute, public incallSvc: IncallService) {
        this.visible = false; // set toolbar visible to false
      }
    
      ngOnInit() {
        this.router.events
          .pipe(
            filter(event => event instanceof NavigationEnd),
            map(() => this.activatedRoute),
            map(route => {
              while (route.firstChild) {
                route = route.firstChild;
              }
              return route;
            }),
          )
          .pipe(
            filter(route => route.outlet === 'primary'),
            mergeMap(route => route.data),
          )
          .subscribe(event => {
            this.viewedPage = event.title; // title of page
            this.showToolbar(event.toolbar); // show the toolbar?
          });
      }
    
      showToolbar(event) {
        if (event === false) {
          this.visible = false;
        } else if (event === true) {
          this.visible = true;
        } else {
          this.visible = this.visible;
        }
      }
    

    toolbar.html

    
      
        {{viewedPage | titlecase}}
      
    
    

提交回复
热议问题