首页 > 解决方案 > Typescript 函数不运行由 Parent 传递的 Child 触发的局部变量

问题描述

我的代码有问题。当我在 C# 中执行此操作时,它可以工作,但在打字稿中它不会。我有一个组件用作组件的构建器,在这种情况下是一个可供不同组件使用的列表,问题是我可以将函数从父级传递给子级,并且它是由子级触发的,但是parent 全部未定义或为空。

这是我的代码:

主要模块.ts:(父)

testVar: string = 'testing';
GotoSalesOrder(event: any) {
   console.log(event); // not null since its a parameter
   console.log(this.testVar); // null or undefiend
   this.router.navigate(['sales-order-header', {id: event}]); // is declared in constructor, and this.router is undefined
}

let rows: any[] = [
  {
    type: 'checkbox',
    variableName: 'testcheckbox',
  },
  {
    type: 'button',
    variableName: 'test1Var',
    OnClick: this.OnClickFunction
  },
  {
    type: 'text',
    variableName: 'test2Var'
  },
  {
    type: 'text',
    variableName: 'test3Var'
  }
];

主模块.html:(父)

<app-table-list [tableContents] = "tableContents"></app-table-list>

app-table-list.ts:(儿童)

@Input() tableContents: TableContents = new TableContents();

app-table-list.html:(儿童)

<button *ngIf = "data.type == 'button'" class="btn btn-link" (click)="data.OnClick(row[data.variableName])">

TableContents.ts 类:

 rows: any[] = [];

标签: angulartypescript

解决方案


推荐阅读