在农业网格事件中,例如onRowSelected(),“ this”是指网格对象。但是,我需要引用组件变量并且不知道该怎么做。我所做的就是这个,但这是一个hack:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
有两种方法可以做到这一点:
onRowSelected: this.onRowSelected.bind(this)onRowSelected与组件紧密耦合,并且只能与该网格一起使用,则此方法很有用。 但是,如果您希望在多个网格之间共享一个功能,并且可以说在网格实用程序服务中具有此功能。
然后,您可以使用以下方法。在gridOptions中,使用上下文选项
gridOptions = { context : {parentComponent: this}...}
和 onRowSelected: this.gridUtilityService.onRowSelected
onRowSelected您可以在其中使用:
const ctx = params.context.parentComponent来引用上下文以引用组件变量
| 归档时间: |
|
| 查看次数: |
257 次 |
| 最近记录: |