[英]How to get current component name in Angular2
我想在用戶點擊促銷頁面時在Angular2中隱藏頁眉和頁腳。
https://example.com/promo
我的應用程序組件如下:
<login-bar></login-bar>
<banner *ngIf="showComponent()"></banner>
<navbar *ngIf="showComponent()"></navbar>
<router-outlet></router-outlet>
<footer *ngIf="showComponent()"></footer>
如果用戶在頁面上:/ promo我想隱藏標題,導航欄和頁腳組件。
在我的代碼中,我有:
export class AppComponent { constructor(
private router:Router
) {}
showComponent(): Boolean {
****
How do I check the current route?
****
var currentRoute = ** enter magic here **
return (currentRoute === "promo") ? true : false;
}
}
所以我的問題是,找到當前所在頁面/路線以便顯示或隱藏組件的最佳方法是什么?
要獲取當前激活的路由,可以注入ActivatedRoute
服務。 這將使您可以訪問當前路線URL的Observable。
export class AppComponent {
// Bind to this:
isPromo = false;
constructor(private route: ActivatedRoute) {}
ngOnInit() {
route.url.subscribe(segments => {
this.isPromo = segments[0].path === "promo";
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.