ag 网格未将上下文传递给 angular 中的子组件

ag grid not passing context to child component in angular

我在我的 angular 项目中使用 Ag-grid 版本 26.2.0。我已经参考了官方文档并按照他们建议的方式实现了代码,以将父上下文传递给子组件。我已执行所有步骤,但父上下文未定义。

我不确定我在这里遗漏了什么。感谢任何帮助。

以下是代码片段:

父组件:

export class CartComponent implements OnInit {
  public frameworkComponents;
  context: any = { comp: this};
...

constructor(){
this.context = { parent: this };
}
...

}

组件 html:

...

    <ag-grid-angular [context]="context" [columnDefs]="savedCartColumns" [rowData]="savedProducts" [pagination]="true"
      [frameworkComponents]="frameworkComponents" [paginationPageSize]="itemsPerPage"
      [suppressDragLeaveHidesColumns]="true" [enableCellTextSelection]="true" [defaultColDef]="defaultColDef"
      [suppressExcelExport]="false" [suppressContextMenu]="false" [popupParent]="popupParent"
      class="ag-theme-balham saved-cart" style="width: 100%; height: 550px" (gridReady)="onGridReady($event)"></ag-grid-angular>

...

子组件:

import { ICellRendererParams } from 'ag-grid-community';
...
export class ManageCartComponent {
  private params: ICellRendererParams;
  componentParent: any;
...

  agInit(params: ICellRendererParams): void {
    this.params = params;
    this.componentParent = params.context.parent;
  }

运行时错误:

ERROR TypeError: Cannot read properties of undefined (reading 'parent')
    at ManageCartComponent.push.gxGq.ManageCartComponent.agInit (manageCart.component.ts:50:43)

好吧,我错过了在正确的 aggrid 实例中添加这段代码,因为我在一页中有两个 aggrid 实例:

context="context"

添加后,子组件按预期获取父组件的上下文!