简体   繁体   English

从子组件向父组件传递数据时出错

[英]getting error while passing data from child component to parent component

I am working on an Angular 7 application. 我正在开发Angular 7应用程序。 I am trying to send data from child component to parent component, for that I am using the @Output decorator with an EventEmitter. 我正在尝试将数据从子组件发送到父组件,因为我正在将@Output装饰器与EventEmitter一起使用。

 @Output() token:EventEmitter<any> = new EventEmitter<any>();

I'm getting the error: 我收到错误消息:

ERROR in src/app/header/header.component.ts(96,6): error TS2322: Type 'string' is not assignable to type 'EventEmitter'. src / app / header / header.component.ts(96,6)中的错误:错误TS2322:类型'string'无法分配给类型'EventEmitter'。

Here I am trying to send value: 在这里,我试图传递价值:

token == eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOiI1MmYzZWEyMC0yMDQzLTQ5MjQtODg3Ny0yNTAzZTZkOTZmNTEiLCJleHAiOjE1ODE2NzQ4OTcsImlzcyI6Ik1hZ2ljTWluZCIsImF1ZCI6Ik1hZ2ljTWluZCJ9.DMcMPgM0B2hZRR-qSIQyIG9SDt_q_G9WON3ZtRXX78I
@Output() token: EventEmitter<string> = new EventEmitter<string>(); Initialize :

this.token.emit(tokenValue); // emit the value to the parent

And, in parent .HTML you need to capture the event: 并且,在父.HTML中,您需要捕获事件:

<child (token)="tokenEvent($event)"></child>

Only if you emit the value the parent will recive the token event 仅当您发出该值时,父级才会接收令牌事件

this.token.emit(tokenValue);

And, on parent you need to do something like: 并且,在父级上,您需要执行以下操作:

<child (token)="handleToken($event)"></child>

Example of use: https://angular.io/api/core/EventEmitter#usage-notes 使用示例: https : //angular.io/api/core/EventEmitter#usage-notes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM