在特定路线上隐藏标题组件(Angular 5)

时间:2018-02-19 05:34:27

标签: javascript angular typescript

我有一个标题,我想在某些路线上隐藏

当您在家中时,路线旁边没有任何内容www.mywebsite.com,但是当您登录路线时,路线中始终会有app,因此从现在开始的每个页面都将如下所示{ {1}}基本上我想在路线中有www.mywebsite.com/app/whatever时隐藏我的标题

我试过这样做..但它似乎没有工作

任何帮助都将不胜感激,如果您知道更好的方法,请告诉我们!

component.html

app

component.ts

<home-header *ngIf="router != '/app'"></home-header>

编辑

我试过这样做..

import { Component } from '@angular/core';
import { Router } from '@angular/router';


export class RootComponent {
     router: string;

     constructor(
        private _router: Router
     ) {
         this.router = _router.url;
     }
}

那不起作用

我也尝试过这样做..

<home-header *ngIf="router.indexOf('app') > -1"></home-header>

3 个答案:

答案 0 :(得分:5)

我能够从http://stackoverflow.com/a/41684866/4194436

找到解决方案
<home-header *ngIf="!_router.url.includes('app')"></home-header>

所以基本上如果路线不包含app显示home-header

答案 1 :(得分:1)

尝试:

*ngIf="router.indexOf('app') > -1"

希望这有帮助

答案 2 :(得分:1)

您只设置路由器变量一次,您应该为变量添加订阅:

constructor(private _router: Router) {
   // this.router = _router.url;
   this._router.events.subscribe(()=> this.router = this._router.url );
}

您可以找到更多相关信息here

不要忘记取消订阅组件销毁以避免内存泄漏。