javascript - 在某些路线上隐藏标题组件(Angular 5)

标签 javascript angular typescript

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

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

我试过了,但是好像不行

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

component.html

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

component.ts

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>

那没用

我也试过这样做..

 export class RootComponent {
    router: string;

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

最佳答案

我能够从 http://stackoverflow.com/a/41684866/4194436 找到解决方案

在你的component.ts

import { Router } from '@angular/router'

//...

constructor(
   public router: Router
) 

然后在你的 component.html

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

所以基本上,如果路由 url 不包含“app”,则显示 home-header 组件。

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

关于javascript - 在某些路线上隐藏标题组件(Angular 5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48860039/

相关文章:

javascript - 如何将数据转换为 JSON

javascript - 隐藏 DIV 并在提交时显示图像

javascript - RxJS 对多个流进行过滤

angular - 如何保留ag网格的状态gridOptions

typescript - 您可以根据类型检查对象,而不将其分配给该类型吗?

typescript - 如果可用则让 Zod 解析,如果不可用则跳过元素

function - 定义 TypeScript 变量类型函数或字符串

javascript - 使用 xpath.js NPM 模块解析 XML

javascript - 为什么javascript按下回车按钮没有功能?

css - Angular 2 - 到处都是重复的 &lt;style&gt; block