[英]How to detect path in Angular2
我想嘗試做類似的事情: AngularJS根據url / condition顯示div
<span id="page-locator" *ngIf="location.path() == '/overview'">test</span>
但它不起作用。
我怎樣才能在Angular2中實現這一目標?
謝謝。
看起來你正試圖使用Angular路由器的路徑,對嗎? 如果是這樣,您可以將Router
服務注入組件並在視圖中使用它:
import { Router } from '@angular/router';
export class SomeComponent {
constructor(public router: Router) { }
}
然后在組件模板中,您可以通過以下方式訪問當前路由器URL:
<span id="page-locator" *ngIf="router.url === '/overview'">test</span>
window.location
是您可以獲取信息的地方!
對於絕對路徑,你可以這樣:
constructor(location:Location) {
console.log(location.prepareExternalUrl(location.path()));
}
嘗試以下解決方案:
location: any;
constructor(private _router: Router) {
_router.events.subscribe((data:any) => { this.location = data.url; });
}
數據是一個對象,我們需要該對象中包含的url屬性。
<span id="page-locator" *ngIf="location == '/overview'">test</span>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.