我在检查/
角度路径时遇到问题。如果路径具有/something
,则需要在菜单上设置活动类。
这是代码示例:
setActiveFlag() {
if (window && window.location) {
const activeRoute = window.location.hash || window.location.pathname;
console.log(activeRoute);
this.nav.forEach(item => {
item.active = false;
/*
If item matches url path active to true
*/
if (item.state.indexOf(activeRoute) !== -1 && activeRoute.match('/')) {
this.selectedItem = item;
item.active = true;
}
});
}
}
这里是组成部分:
<div [ngClass]="{open: navService.sidebarState.sidenavOpen}" class="sidebar-left rtl-ps-none"
[perfectScrollbar]="{suppressScrollX: true}" *ngIf="isAuthenticated()">
<div class="logo">
<img src="../../../../webapp/content/scss/images/logo.png" alt="">
</div>
<ul class="navigation-left">
<li [ngClass]="{active: item.active}" (click)="onClickChangeActiveFlag(item)" class="nav-item lvl1"
*ngFor="let item of nav">
<div class="nav-item-hold" routerLink="{{item.state}}" (click)="closeChildNav()" *ngIf="item.type === 'link'">
<i [class]="'nav-icon '+ item.icon "></i>
<span class="nav-text">{{item.name}}</span>
</div>
<div class="nav-item-hold" (mouseenter)="selectItem(item)" *ngIf="item.type === 'dropDown'">
<i [class]="'nav-icon '+ item.icon "></i>
<span class="nav-text">{{item.name}}</span>
</div>
<div class="nav-item-hold" *ngIf="item.type === 'extLink'">
<a [href]="item.state" target="_blank">
<i [class]="'nav-icon '+ item.icon "></i>
<span class="nav-text">{{item.name}}</span>
</a>
</div>
<div class="triangle"></div>
</li>
</ul>
</div>
我想要的是如果activeRoute
仅匹配/
路径而不匹配/something
。有什么建议吗?