gpt4 book ai didi

angular - 父子组件如何通过点击事件进行通信

转载 作者:行者123 更新时间:2023-12-05 01:39:33 26 4
gpt4 key购买 nike

我有一个 Angular 8 应用程序。我有父子关系。

所以我在父 DossierCorrespondenceComponent 中有一个函数,但该函数必须在子组件中触发。

所以我在父类中有这个函数:

@Input()  myGotoItem: Function;


gotoItem(index, type: string) {
this.showingSingle = true;

switch (type) {
case 'correspondence': {
this.single = this.correspondenceEntries[index];
break;
}
case 'attachments': {
this.single = this.attachmentEntries[index];
break;
}
default: {
break;
}
}
this.showingSingle = true;
}

然后我尝试在子组件 DossierCorrespondenceListComponent 中这样调用它:

 <tr class="dossier-correspondencerow" *ngFor="let entry of correspondenceEntries; let i = index" (myGotoItem)="gotoItem(i, entry.type)">

但什么也没发生

那么我必须改变什么?

但是我还有一个其他组件 DossierCorrespondenceAttachmentsComponent 也使用该功能:

 <tr class="dossier-correspondencerow" *ngFor="let entry of attachmentEntries; let i = index" (click)="gotoItem(i, entry.type)">

因此,如果我必须将函数 gotoItem(index, type: string) 移动到两个子组件,那么我就会有重复的代码。

我现在在父组件中是这样的:

<ng-container *ngIf="correspondenceEntries">
<app-dossier-correspondence-list [correspondenceEntries]="correspondenceEntries" (onClick)="gotoItem(i, entry.type)"> </app-dossier-correspondence-list>
</ng-container>

和ts父组件:

gotoItem(index, type: string) {
this.showingSingle = true;

switch (type) {
case 'correspondence': {
this.single = this.correspondenceEntries[index];
break;
}
case 'attachments': {
this.single = this.attachmentEntries[index];
break;
}
default: {
break;
}
}
this.showingSingle = true;
}

和子组件:

  <tbody class="dossier-tablebody">
<tr class="dossier-correspondencerow" *ngFor="let entry of correspondenceEntries; let i = index" (click)="gotoItem(i, entry.type)">
<td>{{ entry.date | date:"dd-MM-y" }}</td>
<td>{{ entry.name }}</td>
</tr>
</tbody>



和ts子组件:


export class DossierCorrespondenceListComponent implements OnInit {

@Input()
correspondenceEntries: DossierEntry[];

@Output()
onClick = new EventEmitter();

@Input() showingSingle;

constructor() { }

ngOnInit() {
}

ngOnChanges(changes) { console.log(changes)}

click() {
this.onClick.emit();
}


}

但是我得到了这个错误:

dossier-correspondence-list.component.html:13 ERROR TypeError: _co.gotoItem is not a function
at Object.handleEvent (dossier-correspondence-list.component.html:13)
at handleEvent (core.js:29733)
at callWithDebugContext (core.js:30803)
at Object.debugHandleEvent [as handleEvent] (core.js:30530)
at dispatchEvent (core.js:20520)
at core.js:28942
at HTMLTableRowElement.<anonymous> (platform-browser.js:1009)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:26760)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)

我改变了这个:

 click() {
this.onClick.emit(true);
}

dossier-correspondence-item.component.ts:51 ERROR TypeError: _co.gotoItem is not a function
at Object.handleEvent (dossier-correspondence-item.component.ts:51)
at handleEvent (core.js:29733)
at callWithDebugContext (core.js:30803)
at Object.debugHandleEvent [as handleEvent] (core.js:30530)
at dispatchEvent (core.js:20520)
at core.js:28942
at HTMLTableRowElement.<anonymous> (platform-browser.js:1009)
at

最佳答案

父组件:

<child (onClick)="gotoItem()"></child>

gotoItem() {
//do something
}

子组件:

<tr class="dossier-correspondencerow" *ngFor="let entry of attachmentEntries; let i = index" (click)="click()">

@Output() onClick = new EventEmitter();

click() {
// do something
this.onClick.emit()
}

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

父组件(h​​tml)

<app-dossier-correspondence-list [correspondenceEntries]="correspondenceEntries" (onClick)="gotoItem($event)"> </app-dossier-correspondence-list>

父组件(ts)

import { Component } from '@angular/core';

@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {

gotoItem(e) {
console.log(e)
}

}

子组件

import { Component, Input, Output, EventEmitter } from '@angular/core';

@Component({
selector: 'app-dossier-correspondence-list',
template: `
<table>
<tr *ngFor="let entry of attachmentEntries; let i = index" (click)="click(i, entry.type)">
{{entry.type}}
</tr>
</table>
`
})
export class AppDossierCorrespondenceComponent {

@Input() correspondenceEntries: any;
@Output() onClick = new EventEmitter();

attachmentEntries = [
{ type: 'type1' },
{ type: 'type2' },
{ type: 'type3' },
]

click(i, type) {
this.onClick.emit({
i: i,
type: type
})
}

}

链接:https://stackblitz.com/edit/angular-ev6e5n

关于angular - 父子组件如何通过点击事件进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58112733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com