4

我想让一个子元素触发它自己的点击,并且不响应它具有[routerLink]的父元素,问题是子元素无法在其中运行它delete()的功能,(click)="delete()"只需跟随它的父元素[routerLink](它导航到 /product/:id)

    <div class="item">

<!-- parent element -->
    <a [routerLink]="['/product/'+product.id]">
        <div class="figure">
          <div class="sides">
            <div class="side">
              <div class="card">
<img class="img" [src]="product.thumb"> 
                <div class="content">
                  <div class="header">{{product.name}}</div>
                  <div class="meta"> <span>product.category</span> </div>
                </div>
                <div class="extraContent"> <span class="ui right floated black">

                <!-- child element -->
                <a (click)="delete()" ><i class="red trash outline icon"></i></a></span> <span><i class="deleteProductIcon"></i></span> </div> 
                <!-- child element -->

              </div>
            </div>
          </div>
        </div>
      </a>
<!-- parent element -->

</div>

我试图移动[routerLink]到鞋面div,但它仍然做同样的行为

4

3 回答 3

4

在您的情况下,仅指定 stopPropagation 的孩子有时可能不起作用。特别是当孩子和父母都定义了各自的“路由器链接”时

我建议在组件中使用“导航”方法处理路由,最后返回“false”。这将阻止父路由在子项的单击处理程序完成后触发。

// pass in the event object to the delete function
<a (click)="delete($event)" ><i class="red trash outline icon"></i></a>

// in the delete function, stop event propagation
delete(event){
    event.stopPropagation();
    this.route.navigate(['child/route/here']);
    return false;
}

从点击处理程序返回 false 阻止了父路由为我工作。

于 2018-03-21T11:35:55.267 回答
2

如果您在删除函数()中停止Propgation,则事件不会通过 routerLink 冒泡到锚标记

// pass in the event object to the delete function
<a (click)="delete($event)" ><i class="red trash outline icon"></i></a>

// in the delete function, stop event propagation
delete(event){
    event.stopPropagation();
}
于 2017-06-23T17:19:48.367 回答
2

唯一的event.stopPropagation();,对我不起作用。我所要做的就是也e.preventDefault();进去

// In your HTML   
<a [routerLink]="['/product/'+product.id]">
     <!-- your other inner stuff -->
     <a (click)="delete($event)" ><i class="red trash outline icon"></i></a>
</a>

// In your component
delete(e){
    e.stopPropagation();
    e.preventDefault();
}
于 2018-11-25T16:33:27.603 回答