【问题标题】:Angular output with parent and child doesn't work in a button带有父子的角度输出在按钮中不起作用
【发布时间】:2022-12-03 14:44:56
【问题描述】:

I\'m passing a button event from the child to the parent, but when I click the button, nothing happens.

This is the child code

product-item.component.html

 <button (click)=\'handleShowDetail()\' class=\"product_buttonAdd\">

product-item.component.ts

 @Input()
  productItem!: Product;
  @Output()
  showDetail: EventEmitter<string>=new EventEmitter()
handleShowDetail(){
    
    this.showDetail.emit(this.productItem.sku)

 }

This is the code where the product-item is contained

product-list.component.html

<div class=\"header__searchResultsContainer\">
  <div
    class=\"header__searchResults\"
   
  >
    <lpr-product-item
    
    (showDetail)=\"handleShowDetail($event)\"
    ></lpr-product-item>
  </div>
</div> 

product-list.component.ts

 @Output()
  showDetail: EventEmitter<string>=new EventEmitter()
handleShowDetail(sku: string){
    this.showDetail.emit(sku)

  } 

This is the parent who should activate the navigate and target the product based on its sku

product-container.component.html

<div class=\"product_container\">
  <div><lpr-products-list 
        (showDetail)=\"handleShowDetail($event)\"
    
    ></lpr-products-list></div>
  <div></div>
</div>
 

product-container.component.ts

 handleShowDetail(sku: string){
    this.router.navigate([ \'/product-details\' ])
  }

When I click on the button, the user should be redirected to a product detail, but nothing happens

  • First off, product-item passes the SKU in the output but product-list does not use it nor send it, so the sku information is lost. Even knowing that, the router should still navigate, unless the route is unknown? Could you create a fiddle or something to reproduce the behavior?
  • Code as posted should work, some minor issues with not passing $event upwards
  • make sure you declare all your components in the same module

标签: angular typescript frontend


【解决方案1】:
【解决方案2】:
猜你喜欢
  • 2015-12-10
  • 2014-03-19
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-18
  • 2011-04-28
相关资源
最近更新 更多