无法从打字稿中的异步方法发出事件

时间:2019-09-02 11:45:50

标签: angular typescript asynchronous eventemitter

我有一个应该发出事件的子组件。但是,我希望父处理程序方法为 link_directories(${HOME}/lib/${TARGET}) if("${ENVIRONMENT}" STREQUAL "WINDOWS") link_libraries(mylib1.dll mylib2.dll mylib3.dll) 。在这种情况下,父对象似乎没有收到发出的对象:

父组件

async

子组件

<computer-create-modal (onsubmit)="handleCreateAsync($value)"
                       >
          </computer-create-modal>

export class IndexComponent implements OnInit {
async handleCreateAsync(computer:Computer){
    console.log(computer); //always undefined
    if(computer=null && computer.id !=null){

    }

  }
}

}

我尝试使子方法同时发出事件和异步事件,而不发出事件。 为什么我父母没有得到价值?

PS 我没有添加孩子<input class="form-control" type="text" [(ngModel)]="id" name="id" id="id"/> <input class="form-control" type="text" [(ngModel)]="username" name="username" id="name"/> export class ComputerCreateModalComponent implements OnInit { @Output() protected onsubmit:EventEmitter<Computer>=new EventEmitter<Computer>(); protected username:string=""; protected id:string=""; async onSubmitAsync(){ let comp:Computer={ id:this.id, username:this.username }; console.log("from child"); console.log(comp); this.onsubmit.emit(comp); } 的逻辑,因为在这种情况下无关紧要。

1 个答案:

答案 0 :(得分:0)

这是一个愚蠢的问题。问题是我没有遵循使用(...)=method($event)的约定,而是写了$value

因此,绑定孩子的输出处理程序时,问题出在父级:

<computer-create-modal  (onsubmit)="handleCreateAsync($value)"></computer-create-modal>

$value更改为$event解决了这个问题。