Tengo un componente para el diseñador de informes y asigno un método al evento onSaveReport , pero en ese método mi miembro del componente no está undefined
options: any; designer: any; public document: ReportDocument; reportBody = ''; constructor() { } ngOnInit() { this.document = new ReportDocument(); this.options = new Stimulsoft.Designer.StiDesignerOptions(); this.options.appearance.fullScreenMode = false; this.designer = new Stimulsoft.Designer.StiDesigner(this.options, 'StiDesigner', false); var report = new Stimulsoft.Report.StiReport(); var json = { "DataSet": [{ Id: "a", Name: "A" }] } var dataSet = new Stimulsoft.System.Data.DataSet("JSON"); dataSet.readJson(json) report.regData("JSON", "JSON", dataSet); report.dictionary.synchronize(); this.designer.report = report; this.designer.renderHtml('designer'); this.designer.onSaveReport = this.onReportSave; } private onReportSave(e: any) { this.document.body = e.report.saveToJsonString(); // error is here } }este es un error:
No se pueden establecer propiedades de indefinido (configurando 'cuerpo') TypeError: No se pueden establecer propiedades de indefinido (configurando 'cuerpo') en $.onReportSave [como onSaveReport] (http://localhost:4200/features-stimulsoft-report-stimulsoft-report -module.js:1206:28) en $.invokeSaveReport (http://localhost:4200/assets/lazyBundles/stimulsoft/stimulsoft.designer.js:30:82619) en $.raiseCallbackEventAsync (http://localhost:4200 /assets/lazyBundles/stimulsoft/stimulsoft.designer.js:30:86029) en http://localhost:4200/assets/lazyBundles/stimulsoft/stimulsoft.designer.js:30:78818 en ZoneDelegate.invokeTask (http:// localhost:4200/polyfills.js:12178:35) en Object.onInvokeTask (http://localhost:4200/vendor.js:72788:33) en ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js: 12177:40) en Zone.runTask (http://localhost:4200/polyfills.js:11946:51) en invocarTask (http://localhost:4200/polyfills.js:12259:38) en ZoneTask.invoke (http ://localhost:4200/polyfills.js:12248:52)`
No tiene acceso a su clase, this dentro de la función onReportSave porque pasa la referencia de su función a onSaveReport y dado que el diseñador llama a su función, this dentro de su función pertenece al diseñador, no a su clase, y esa es la causa del error. Este documento no está definido. y llama al cuerpo desde un indefinido, pero puede cambiarlo con la propiedad de vinculación
this.designer.onSaveReport = this.onReportSave.bind(this); Ahora configura this dentro de su función para que sea this de su clase.