如何在 *ngIf 中捕获路由
How to catch routes in *ngIf
所以我想在点击特定页面时让 Header 上的锚元素消失。当该页面被点击时,如何在 *ngIf
中捕获 url。
我有一个 header,所有页面都将保持不变。当我在 /home
被路由时,只需要隐藏一个锚元素。如何在 *ngIf
中捕获此 "/home"
?
*ngIf = "href='/home'"
无效。还有其他选择吗?
mycomponent.component.ts
import { Router } from '@angular/router'
class MyComponent {
constructor(public router: Router) { }
}
mycomponent.component.html
<div *ngIf="router.url === '/some/route'">
<!-- … -->
</div>
// mycomponent.component.ts
class MyComponent {
constructor(public router: Router){
}
}
// mycomponent.component.html
<div *ngIf="this.router.url === '/some/route'">
</div>
我来到这个页面是因为我遇到了与 Ravy 相同的问题,但建议的解决方案对我来说并不合适。
如果您使用带有查询参数的路由路径,则接受的答案无效。
这是我使用的解决方案:
mycomponent.component.ts
import { Router } from '@angular/router'
class MyComponent {
constructor(public router: Router) { }
}
mycomponent.component.html
<div *ngIf="router.isActive('/some/route')">
<!-- … -->
</div>
所以我想在点击特定页面时让 Header 上的锚元素消失。当该页面被点击时,如何在 *ngIf
中捕获 url。
我有一个 header,所有页面都将保持不变。当我在 /home
被路由时,只需要隐藏一个锚元素。如何在 *ngIf
中捕获此 "/home"
?
*ngIf = "href='/home'"
无效。还有其他选择吗?
mycomponent.component.ts
import { Router } from '@angular/router'
class MyComponent {
constructor(public router: Router) { }
}
mycomponent.component.html
<div *ngIf="router.url === '/some/route'">
<!-- … -->
</div>
// mycomponent.component.ts
class MyComponent {
constructor(public router: Router){
}
}
// mycomponent.component.html
<div *ngIf="this.router.url === '/some/route'">
</div>
我来到这个页面是因为我遇到了与 Ravy 相同的问题,但建议的解决方案对我来说并不合适。
如果您使用带有查询参数的路由路径,则接受的答案无效。
这是我使用的解决方案:
mycomponent.component.ts
import { Router } from '@angular/router'
class MyComponent {
constructor(public router: Router) { }
}
mycomponent.component.html
<div *ngIf="router.isActive('/some/route')">
<!-- … -->
</div>