7中渲染组件之前加载数据

7中渲染组件之前加载数据

本文介绍了在Angular 7中渲染组件之前加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在渲染组件之前,我试图从服务中恢复数据,但出现此错误:TypeError:无法读取未定义的属性'dataEntries'

I tried to recuperate data from service before rendering my component but i have this error: TypeError: Cannot read property 'dataEntries' of undefined

这是我的代码:

ngOnInit() {
 this.route.params.subscribe((params:Params)=>{
  this.ActivityId=params['id']
 })
 this.activityInstanceIdentifier= {
  "class":"eu.w4.engine.client.bpmn.w4.runtime.ActivityInstanceIdentifier",
  "id":this.ActivityId
 }
 this.activityInstanceAttachement= {
  "class":"eu.w4.engine.client.bpmn.w4.runtime.ActivityInstanceAttachment",
  "dataEntriesAttached":true
 }
this.activityService.getActivityInstance(this.sessionService.getPrincipal(),
                                         this.activityInstanceIdentifier,
                                         this.activityInstanceAttachement)
                                        .subscribe((ActivityInstance)=>{
                                          this.dataInstance=ActivityInstance
                                        });
 }

forms = [
 {
  dataEditionMode:DataEditionMode.DISPLAY,
  name:"demande",
  editedInstance:this.dataInstance["dataEntries"]["demande"]["value"],
  component:DemandeFormComponent,
  multiple:false
 }
]

我也尝试使用'resolve',但是它不起作用,有帮助吗?

I tried to use 'resolve' also but it doesn't work , any help ?

推荐答案

在您的情况下, getActivityInstance 方法是异步的,因此JS不会等待它完成,它将执行下一行.因此

In you case getActivityInstance method is asynchronous hence JS won't wait for it to get complete and it will execute the next line . Hence

editedInstance:this.dataInstance ["dataEntries"] ["demande"] ["value"] 在服务返回数据之前执行(此时 dataInstance 将是 undefined (如果尚未初始化).

editedInstance : this.dataInstance["dataEntries"]["demande"]["value"] gets executed before service returns the data (that time dataInstance will be undefined if you haven't initialized it) .

像这样修改您的代码:

this.activityService.getActivityInstance(this.sessionService.getPrincipal(),
                                         this.activityInstanceIdentifier,
                                         this.activityInstanceAttachement)
                                        .subscribe((ActivityInstance)=>{
                                          this.dataInstance=ActivityInstance;

 forms = [
 {
  dataEditionMode:DataEditionMode.DISPLAY,
  name:"demande",
  editedInstance:this.dataInstance["dataEntries"]["demande"]["value"],
  component:DemandeFormComponent,
  multiple:false
 }
]
  });

这篇关于在Angular 7中渲染组件之前加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:30