如何在Angular 2中访问没有参数的routerlink

问题描述 投票:1回答:1

我很难在Angular 2中访问没有参数的routerlink。我需要路由器链接来检查我是否要在导航中显示某个元素。对于没有参数的普通路由器链接,我这样做:

*ngIf="router.url === '/about'"

现在我有/link/category等参数的链接,类别是参数,我想检查routerlink是否是/link/*。我怎么做到最好?

angular typescript routing
1个回答
0
投票

获取网址,如果它包含'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'

© www.soinside.com 2019 - 2024. All rights reserved.