angular 2 - how to hide nav bar in some components

后端 未结 9 719
半阙折子戏
半阙折子戏 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:27

    Navbar control and formatting is often needed throughout an app, so a NavbarService is useful. Inject in those components where you need.

    navbar.service.ts:

    import { Injectable } from '@angular/core';
    
    @Injectable()
    export class NavbarService {
      visible: boolean;
    
      constructor() { this.visible = false; }
    
      hide() { this.visible = false; }
    
      show() { this.visible = true; }
    
      toggle() { this.visible = !this.visible; }
    
      doSomethingElseUseful() { }
    
      ...
    }
    

    navbar.component.ts:

    import { Component } from '@angular/core';
    import { NavbarService } from './navbar.service';
    
    @Component({
      moduleId: module.id,
      selector: 'sd-navbar',
      templateUrl: 'navbar.component.html'
    })
    
    export class NavbarComponent {
    
      constructor( public nav: NavbarService ) {}
    }
    

    navbar.component.html:

    
    

    example.component.ts:

    import { Component, OnInit } from '@angular/core';
    import { NavbarService } from './navbar.service';
    
    @Component({
    })
    export class ExampleComponent implements OnInit {
    
      constructor( public nav: NavbarService ) {}
    }
    ngOnInit() {
      this.nav.show();
      this.nav.doSomethingElseUseful();
    }
    

提交回复
热议问题