javascript - Angular 7 : passing parameters to other component error

标签 javascript angular typescript events components

我有 2 个组件,一个父组件和一个子组件,我想将变量的值发送到父组件,我尝试了下面的代码但没有成功。子组件正确发送了信息,但父组件没有接收到,看来父组件中的函数没有识别到​​要触发的数据的接收。

子组件

...

private elements:any;
@Output() element = new EventEmitter<any>();


 constructor() {

 }

  ngOnInit() {
    this.sendContent();
  }


 sendContent() {
    this.elements = "hi";

    console.log("sended");
    console.log(this.elements);

    this.element.emit(this.elements);
    //the function is activated and I can see the return in the console
  }

父组件

...

constructor() {

}

ngOnInit() {

}

receiveContent(elements) {
    console.log("received");
    console.log(elements);
    //the function is not activated when the child component is sent the data
 }

父模板

<app-child (sendContent)="receiveContent($event)"></app-child>

谢谢。

最佳答案

在括号内,您应该放置用 @Output 装饰器装饰的属性的名称。在本例中(元素)

将您的parent.html更改为:

<app-child (element)="receiveContent($event)"></app-child>

关于javascript - Angular 7 : passing parameters to other component error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54828434/

相关文章:

java - 结合 JSP servlet 和 Javascript

javascript - Vue-konva 遇到错误 : Must use import to load ES Module

javascript - 将生成的 Javascript 事件添加到生成的 HTML

typescript - 三个JS中如何动态改变顶点颜色

typescript - 为什么 Typescript 不能在赋值中使用泛型的 keyof 类型并将其泛化为字符串?

javascript - AdGroupCriterionService 不会更改关键字文字

angular - 按下按钮时 Ionic2 关闭 fab 菜单

Angular2 bool 输入参数不起作用

javascript - 链接 Angular 和 Node.js : TypeError: error. json 不是函数

javascript - 一旦我设置了nodeIntegration :false -> “Uncaught ReferenceError: require is not defined at Object.url (external ” url“:1) ” #electron-#react-#typescript