【问题标题】:Accessing RouterLinkActive in a nested component using Angular 6使用 Angular 6 在嵌套组件中访问 RouterLinkActive
【发布时间】:2018-10-15 03:32:40
【问题描述】:

我有一个列表组,定义为 recipe-items 列表。我正在使用子路由,以便在用户单击列表元素时显示项目的描述。到目前为止,点击事件和路由都在工作,但 我想将点击的项目标记为活动

recipe-list.component.html

<app-recipe-item 
  *ngFor="let recipeEl of recipes; let i = index" 
  [recipe]="recipeEl"
  [routerLink]="i"
  style="cursor: pointer;"
  >
</app-recipe-item>

为了做到这一点,我尝试在嵌套的 RecipeItemComponent 中使用 routerLinkActive 指令,但看起来该指令超出了嵌套组件的范围。

recipe-item.component.html

<div class="list-group">
  <a 
    class="list-group-item list-group-item-action d-flex justify-content-between align-items-start"
    routerLinkActive="active"
    >
     TO BE MARKED AS ACTIVE WHEN CLICKED
  </a>
</div>

我错过了什么?即使使用 localRef 也无法在嵌套组件中检索其值。

【问题讨论】:

    标签: angular angular2-routing angular6


    【解决方案1】:

    使用RouterLinkActive 指令及其属性isActive

    使用RouterLinkActive 和本地引用,可以将isActive 的值传递给嵌套组件的@Input() 属性,以便在其模板中使用它来触发ngClass

    recipe-list.component.html

     <app-recipe-item 
      *ngFor="let recipeEl of recipes; let i = index" 
      [recipe]="recipeEl"
      [routerLink]="i"
      routerLinkActive
      #rla="routerLinkActive"
      [currentlySelected]="rla.isActive"
      style="cursor: pointer;"
      >
    </app-recipe-item>
    

    recipe-item.component.ts

    @Component({
      selector: 'app-recipe-item',
      templateUrl: './recipe-item.component.html',
      styleUrls: ['./recipe-item.component.css']
    })
    export class RecipeItemComponent implements OnInit {
      @Input() recipe: Recipe;
      @Input() currentlySelected: boolean;
    

    recipe-item.component.html

    ...
    <a [ngClass]="{active: currentlySelected}">
    ...
    

    【讨论】:

      【解决方案2】:

      routerLinkActive 指令通过订阅路由器的导航事件为活动链接添加特殊样式,请参阅 source code

      您也可以在 app-recipe-item 组件中执行相同的操作,而无需使用 routerLinkActive 指令。(代码相同)


      另一种方式,routerLinkActive 提供了一个 isActive 属性,它显示当前 routerLink 是否处于活动状态。您也可以将其作为组件注入,以检索其值并更改为活动样式。

      <app-recipe-item 
        *ngFor="let recipeEl of recipes; 
        let i = index" [recipe]="recipeEl" 
        [routerLink]="i" style="cursor: pointer;"
        routerLinkActive
      >
      </app-recipe-item>
      
      constructor(
        @Inject(RouterLinkActive) private activeRouter: RouterLinkActive  // inject
      ) { }
      
      <a class="list-group-item list-group-item-action d-flex justify-content-between align-items-start"
        [ngClass]="{active: activeRouter.isActive}"
      >
        TO BE MARKED AS ACTIVE WHEN CLICKED
      </a>
      

      请参考 demo

      【讨论】:

      • 谢谢@Pengyy。我的实际解决方案涉及使用isActive,但为了方便使用它,我必须将此布尔值传递给子组件recipe-item。因此,recipe-list 利用了RouterLinkActive 指令,将布尔属性设置为recipe-item => routerLinkActive #rla="routerLinkActive" [currentlySelected]="rla.isActive"
      • 我在下面添加了我的个人解决方案,仅供参考,我将接受您的回答作为有效解决方案。 ;)
      • @sentenza 谢谢!顺便说一句,很好的解决方案。
      猜你喜欢
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      • 2020-11-11
      • 1970-01-01
      相关资源
      最近更新 更多