Cómo pasar 2 parámetros a EventEmitter angular2
Tengo en mi componente un EventEmitter
pero no puedo compilar porque devuelve este error:
"Los parámetros suministrados no coinciden con ninguna firma de destino de llamada"
Mi componente:
@Output() addModel = new EventEmitter<any>();
saveModel($event, make, name) {
this.addModel.emit(make, name);
}
Si elimino uno de los parámetros en this.addModel.emit()
funciona, pero entonces, ¿puedo pasar 2 parámetros y cómo, a mi EventEmitter?
Traté también con:
@Output() addModel = new EventEmitter<any,any>();
Pero no funciona
3 answers
Si nos fijamos en el método EventEmitter
de la API emit
, solo puede tomar un solo parámetro de tipo T
Emit (value?: T)
Dado que solo se permite un solo parámetro, considere pasar el parámetro como en object
en el método emit. Del mismo modo en el siguiente método make
& name
variable están sosteniendo sus valores respectivos.
this.addModel.emit({make: make, name: name});
//shorthand is below
this.addModel.emit({make, name});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-15 05:55:29
Lo arreglé haciendo
EventEmitter<object>();
Entonces pude pasar un objeto como:
this.MyOutputVariable.emit({ name: 'jack', age: '12' });
Y funcionó.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-15 11:31:42
Otra opción para escribir fuertemente es la siguiente:
@Output addModel = new EventEmitter<{make: string, name: string}>();
Luego puedes emitirlo como muestra @ Pankaj-Parkar
this.addModel.emit({make, name});
othis.addModel.emit({make: 'honda', name: 'civic'});
Ahora tiene una escritura fuerte en lugar de usar object
o any
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-27 19:42:15