How pass 2 parameters to EventEmitter angular2

匿名 (未验证) 提交于 2019-12-03 02:55:01

问题:

I have in my component a EventEmitter but I can't compile because return this error: "Supplied parameters do not match any signature of call target"

My component:

@Output() addModel = new EventEmitter<any>();  saveModel($event, make, name) {     this.addModel.emit(make, name); } 

If i delete one of parameters in this.addModel.emit() it work, but so, Can i pass 2 parameters and how, to my eventEmitter?

I tried also with :

@Output() addModel = new EventEmitter<any,any>(); 

but It doesn't work

回答1:

If you look at the EventEmitter API's emit method, it can only take single parameter of type T

emit(value?: T)

Since only single parameter is allowed, consider passing parameter as in object in emit method. Likewise in below method make & name variable are holding their respective values.

this.addModel.emit({make: make, name: name}); //shorthand is below this.addModel.emit({make, name}); 


回答2:

I fixed it by making

EventEmitter<object>(); 

Then I was able to pass an object such as:

this.MyOutputVariable.emit({ name: 'jack', age: '12' }); 

And it worked.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!