带有父变量的Angular Type脚本未定义错误

时间:2019-12-06 17:06:20

标签: angular typescript

任何人都明白为什么我会收到未定义的错误吗?

export abstract class BaseEditorComponent implements IPropertyEditor, OnDestroy {

  @Input()
  public element: BpmnJS.IRegistryElement; 

--more code here


export class CommonPropertiesEditorComponent extends BaseEditorComponent {

  constructor(
  ) {
    super();
  }

  public get elementId(): string {
    return this.element.id;
  } 

export class ExclusiveGatewayEditorComponent extends CommonPropertiesEditorComponent implements OnInit {

  public model: IUserTaskDef;
  public outGoing: Sequences[];

  constructor(
    private service: ExclusiveGatewayService
  ) {
    super();
    this.model = {} as any;
    this.outGoing = this.getOutGoingSequences();
  }

  public getOutGoingSequences(): Sequences[] {

    return this.service.getOutgoingSequences(this.elementId); //This is undefined.
  }
  

TypeError:无法读取未定义的属性“ id”       在ExclusiveGatewayEditorComponent.get [作为elementId](common-properties-editor.component.ts:16)       在ExclusiveGatewayEditorComponent.push ../ src / app / properties / editors / gateways / exclusive-gateway / exclusive-gateway-editor.component.ts.ExclusiveGatewayEditorComponent.getOutGoingSequences   (独家网关编辑器.component.ts:27)       在新的ExclusiveGatewayEditorComponent(exclusive-gateway-editor.component.ts:22)       在createClass(core.js:21148)       在createDirectiveInstance(core.js:21027)       在createViewNodes(core.js:29387)       在createRootView(core.js:29301)       在callWithDebugContext(core.js:30309)       在Object.debugCreateRootView [作为createRootView](core.js:29819)       在ComponentFactory_.push ../ node_modules/@angular/core/fesm5/core.js.ComponentFactory_.create   (core.js:20506)

如果我在html页面中输入相同的功能,则可以正常工作并显示element.id的值

<input class="form-control" type="text" name="txtId" [(ngModel)]="elementId" readonly>

1 个答案:

答案 0 :(得分:1)

您不应尝试从构造函数访问@Input()属性,因为这些属性目前不存在。您应该改为使用ngOnInit()方法。

export class ExclusiveGatewayEditorComponent extends CommonPropertiesEditorComponent implements OnInit {

  // ...

  constructor(
    private service: ExclusiveGatewayService
  ) {
    super();
    this.model = {} as any;    
  }

  ngOnInit() {
    this.outGoing = this.getOutGoingSequences();
  }
}