在 ag-grid 事件中,例如 onRowSelected(),'this' 指的是网格对象。但是,我需要引用组件变量并且不知道如何。我所做的是这个,但它是一个黑客:
initializeGridOptions() {
this.gridOptions = {
columnDefs: [
{ headerName: "Core #", field: "coreNumber", width: 100, sort: 'asc' },
onRowSelected: this.onRowSelected,
}
this.gridOptions['_this'] = this; // HACK
}
onRowSelected(event: any) {
if (event.node.selected) {
(this as any)._this.selectedNode = event.node.data;
}
}
有没有更好的办法?