简体   繁体   English

Angular 7:EventEmitter 对点击事件不起作用

[英]Angular 7: EventEmitter does not work on click event

I want to use a EventEmitter to open a new tab and ideally fill in the filter with the clicked reference.我想使用 EventEmitter 打开一个新选项卡,理想情况下使用单击的引用填充过滤器。

So my project is set up as followed:所以我的项目设置如下:

I have a invoice component which is basicly a table showing invoices.我有一个发票组件,它基本上是一个显示发票的表格。 It is a expande table so when a line is clicked, it opens a detailled table with shipment information.它是一个展开表,因此当单击一行时,它会打开一个包含发货信息的详细表。 In the .html file I've added the a click event on a reference of a shipment:在 .html 文件中,我在货件参考中添加了点击事件:

 <!-- Reference Column -->
      <ng-container matColumnDef="reference">
        <th mat-header-cell *matHeaderCellDef> Shipment </th>
        <td mat-cell *matCellDef="let expandedElement" (click)="applyShipmentFilter()"> {{expandedElement.reference}} </td>
      </ng-container>

This click event calls the following function in the invoice.ts file:这个点击事件调用了 invoice.ts 文件中的以下函数:

  applyShipmentFilter(){
console.log("INVOICE COMPONENT: Shipment is clicked w/ reference ");
this.click.emit();
}

So far so good, the console logs INVOICE COMPONENT: Shipment is clicked w/ reference .到目前为止一切顺利,控制台记录 INVOICE COMPONENT: Shipment is clicked w/ reference 。

When the reference is clicked, I want to make sure that the following function is called in the app.component.ts file:单击引用时,我想确保在 app.component.ts 文件中调用以下函数:

  public goToOtherTab(){
    console.log("IN APP COMPONENT: tab changed");
    const tabCount = 4;
    this.selectedIndex = (2) % tabCount;
  }

So I added a eventEmitter in the invoice component:所以我在invoice组件中添加了一个eventEmitter:

@Output() click = new EventEmitter();

And added a click function in the app html file, which I expected to call the goToOtherTab function, but it does not:并在应用程序 html 文件中添加了一个点击函数,我希望调用 goToOtherTab 函数,但它没有:

 <mat-tab label="Shipment costs"> 
    <app-buyers-console-table (click)="goToOtherTab()"></app-buyers-console-table>
  </mat-tab>

Can someone explain to me why this is not working?有人可以向我解释为什么这不起作用吗?

The problem turned out to be that I did not place the (shipmentClicked)="goToOtherTab($event)" in the right place to acces the event.问题原来是我没有将(shipmentClicked)="goToOtherTab($event)"放在正确的位置来访问事件。

When you add a event in an event specific component, in this case the invoice.component.ts, you need to acces the event in the same place in the parent .html.当您在特定于事件的组件中添加事件时,在这种情况下是 invoice.component.ts,您需要在父 .html 的相同位置访问该事件。

In my case I did not, first I've placed it like this:就我而言,我没有,首先我是这样放置的:

<mat-tab-group *ngIf="loginService.isUserLoggedIn()" [(selectedIndex)]="selectedIndex">
  <mat-tab label="Invoices"> 
    <app-invoices></app-invoices>
  </mat-tab>
  <mat-tab label="Shipment costs"> 
    <app-buyers-console-table (shipmentClicked)="goToOtherTab($event)"></app-buyers-console-table>
  </mat-tab>
  <mat-tab label="Container costs">
    <app-single-container></app-single-container>
  </mat-tab>
  <mat-tab label="Ratecard">
    <app-ratecard-change></app-ratecard-change>
  </mat-tab>
</mat-tab-group>

This way the app.component can not acces the event.这样 app.component 就无法访问该事件。 When I changed it to:当我将其更改为:

<mat-tab-group *ngIf="loginService.isUserLoggedIn()" [(selectedIndex)]="selectedIndex">
  <mat-tab label="Invoices"> 
    <app-invoices (shipmentClicked)="goToOtherTab($event)"></app-invoices>
  </mat-tab>
  <mat-tab label="Shipment costs"> 
    <app-buyers-console-table></app-buyers-console-table>
  </mat-tab>
  <mat-tab label="Container costs">
    <app-single-container></app-single-container>
  </mat-tab>
  <mat-tab label="Ratecard">
    <app-ratecard-change></app-ratecard-change>
  </mat-tab>
</mat-tab-group>

it works!有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM