我在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)
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)
<mat-toolbar color="primary" *ngIf="visible">
<mat-toolbar-row>
<span>{{viewedPage | titlecase}}</span>
</mat-toolbar-row>
</mat-toolbar>
Run Code Online (Sandbox Code Playgroud)
添加到丹的答案。
一个完整的答案需要更多的细节。哪个正在从中将其注册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)