Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 隐藏特定路线上的标题组件(角度5)_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 隐藏特定路线上的标题组件(角度5)

Javascript 隐藏特定路线上的标题组件(角度5),javascript,angular,typescript,Javascript,Angular,Typescript,我想在某些路线上隐藏一个标题 当你在家中时,路线旁边没有任何内容,因此www.mywebsite.com,但是当你登录路线时,路线中始终会有app,因此从现在开始,每个页面都会像这样www.mywebsite.com/app/whatever基本上,当路线中有app时,我想隐藏我的标题 我已经试过了。。但它似乎不起作用 任何帮助都将不胜感激,如果你知道更好的方法,请让我知道 component.html <home-header *ngIf="router != '/app'">&l

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

当你在家中时,路线旁边没有任何内容,因此
www.mywebsite.com
,但是当你登录路线时,路线中始终会有
app
,因此从现在开始,每个页面都会像这样
www.mywebsite.com/app/whatever
基本上,当路线中有
app
时,我想隐藏我的标题

我已经试过了。。但它似乎不起作用

任何帮助都将不胜感激,如果你知道更好的方法,请让我知道

component.html

<home-header *ngIf="router != '/app'"></home-header>
<home-header *ngIf="!router.url.includes('app')"></home-header>
编辑

我已经试过了

<home-header *ngIf="router.indexOf('app') > -1"></home-header>
 export class RootComponent {
    router: string;

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


希望这对您有所帮助

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

constructor(private _router: Router) {
   // this.router = _router.url;
   this._router.events.subscribe(()=> this.router = this._router.url );
}
你可以找到更多关于它的信息


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

我从

在您的组件中。ts

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


export class RootComponent {
     router: string;

     constructor(
        private _router: Router
     ) {
         this.router = _router.url;
     }
}
import { Router } from '@angular/router'

//...

constructor(
   public router: Router
) 
然后在组件.html中

<home-header *ngIf="router != '/app'"></home-header>
<home-header *ngIf="!router.url.includes('app')"></home-header>

因此,基本上,如果路由url不包含“app”,则显示
主标题
组件


因此,基本上,如果路线不包括
应用程序
显示
主标题

您可以为您的问题制作一个stackblitz吗?我能够找到答案,
它在IE上不起作用,因为不支持包含。使用indexOf解决方案,我在VS代码中得到了“期望操作符具有类似类型或任何”错误,您可以使用polyfill