我有一个标题,我想在某些路线上隐藏
当你在家时,路线旁边没有任何东西,所以 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/