我很难在Angular 2中访问没有参数的routerlink。我需要路由器链接来检查我是否要在导航中显示某个元素。对于没有参数的普通路由器链接,我这样做:
*ngIf="router.url === '/about'"
现在我有/link/category
等参数的链接,类别是参数,我想检查routerlink是否是/link/*
。我怎么做到最好?
获取网址,如果它包含'link
',则将其评估为true
。
就像是:
import { Router } from '@angular/router';
route;
constructor(router: Router) {
router.events.subscribe((url:any) => this.route = url);
// this.router = "link/category" or something else...
}
checkRoute() {
return this.route.includes('link');
//will return true if this.route has 'link' in it,
//otherwise it will return false.
}
然后只需使用* ngIf ='checkRoute()'查看您所在的路线是否包含'link'