角度2 - 如何隐藏某些组件中的导航栏

Use*_*123 20 html angular

我在nav.component.html中单独创建了导航栏,如何在某些组件中隐藏导航栏,例如login.component.

nav.component.html

<nav class="navbar navbar-default navbar-fixed-top navClass">
    <div class="container-fluid">
        <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed"
                        (click)="toggleState()">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>

        </div>
         <div class="collapse navbar-collapse"
              [ngClass]="{ 'in': isIn }">
          enter code here   <ul class="nav navbar-nav">
               <li class="active"><a href="#">Home</a></li>
               <li><a href="#">about</a></li>

            </ul>

        </div>
    </div>
</nav>
Run Code Online (Sandbox Code Playgroud)

Dan*_*Dan 58

整个应用程序通常都需要Navbar控件和格式,因此NavbarService非常有用.注入您需要的组件.

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() { }

  ...
}
Run Code Online (Sandbox Code Playgroud)

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 ) {}
}
Run Code Online (Sandbox Code Playgroud)

navbar.component.html:

<nav *ngIf="nav.visible">
 ...
</nav>
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

  • 毫无疑问,此解决方案有效。但理想情况下,服务应包含API调用和与HTTP相关的内容,而不是显示/隐藏逻辑! (2认同)
  • 这对 Angular 6 仍然有效吗?我似乎无法使其工作,如果其他组件的 onInit 将其更改为 true,则 nav 组件上的 nav.visible 仍然显示 false。 (2认同)

cbi*_*iau 15

通过向 route.module 中的路由添加数据对象,我能够在不使用导航/工具栏服务的情况下解决此问题。我扩展了Todd Motto 将动态标题添加toolbar: false/true页面并添加到我路径中的数据对象的示例。然后我在我的 toolbar.component 中订阅了路由器事件。使用 Todd 的事件侦听器 func,我读取路径对象并使用布尔值来设置工具栏可见或不可见。

无需服务,可在 pagerefresh 上工作。

路由模块

...
const routes: Routes = [
{ path: 'welcome', component: WelcomeComponent, data: { title: 'welcome', toolbar: false} }, ...];
Run Code Online (Sandbox Code Playgroud)

工具栏组件

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;
    }
  }
Run Code Online (Sandbox Code Playgroud)

工具栏.html

<mat-toolbar color="primary" *ngIf="visible">
  <mat-toolbar-row>
    <span>{{viewedPage | titlecase}}</span>
  </mat-toolbar-row>
</mat-toolbar>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这是我在这里看到的最好的方法。:) (2认同)

Sha*_*era 6

添加到的答案。

一个完整的答案需要更多的细节。哪个正在从中将其注册NavbarService为整个应用程序的提供商app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { SharedModule } from './shared/shared.module';

import { AppComponent } from './app.component';
import { NavbarModule } from './navbar/navbar.module';
import { NavbarService } from './navbar/navbar.service';

import { AppRoutingModule, routedComponents } from './routing.module';

@NgModule({
    imports: [
        BrowserModule, FormsModule, HttpModule,
        NavbarModule,
        SharedModule,
        AppRoutingModule
    ],
    declarations: [
        routedComponents,
    ],
    providers: [
        // Here we register the NavbarService
        NavbarService  
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)