检查角度的根路径

时间:2019-05-24 09:08:21

标签: angular angular7 angular-router

我在检查/角度路径时遇到问题。如果路径具有/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。有什么建议吗?

0 个答案:

没有答案