Angular 无法从typescript中的异步方法发出事件
我有一个子组件,它应该发出一个事件。但是我希望父处理程序方法是Angular 无法从typescript中的异步方法发出事件,angular,typescript,asynchronous,eventemitter,Angular,Typescript,Asynchronous,Eventemitter,我有一个子组件,它应该发出一个事件。但是我希望父处理程序方法是async。在这种情况下,父处理程序似乎没有收到发出的对象: 父组件 <computer-create-modal (onsubmit)="handleCreateAsync($value)" > </computer-create-modal> export class IndexComponent implements OnInit { a
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){
}
}
}
<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);
}
导出类IndexComponent实现OnInit{
异步handleCreateAsync(计算机:计算机){
console.log(计算机);//始终未定义
if(computer=null&&computer.id!=null){
}
}
}
子组件
<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){
}
}
}
<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);
}
导出类ComputerCreateModalComponent实现OnInit{
@输出()
受保护的onsubmit:EventEmitter=新的EventEmitter();
受保护的用户名:string=“”;
受保护id:string=“”;
异步onSubmitAsync(){
让我们来看看计算机={
id:this.id,
用户名:this.username
};
console.log(“来自子项”);
控制台日志(comp);
这个.onsubmit.emit(comp);
}
}
我已经尝试使发出事件的子方法同时同步和异步到no avil。
为什么我的父母没有得到这个值
p.S我没有添加孩子的
submit
逻辑,因为这在本例中无关紧要。这是一个愚蠢的逻辑。问题是我没有遵循使用(…)=method($event)
的惯例,而是编写了$value
因此,在为子对象的输出绑定处理程序时,问题出现在父对象中:
<computer-create-modal (onsubmit)="handleCreateAsync($value)"></computer-create-modal>
将
$value
更改为$event
解决了此问题。它可能与提交上的受保护的关键字有关。
计算机=null
!?你不缺少一个=
?