@Output childEvent未初始化

时间:2019-02-16 21:27:40

标签: javascript angular typescript

我对Angular领域还不是很陌生,并且正在使用angular 7,我正在做一些示例,其中的组件共享数据,Parent to child部分工作正常,但是当我尝试将任何值从child发送给Parent时,工作。

它向我显示此错误enter image description here

app-component.html

<div>{{show}}</div>

<app-communicating-components [Parent]="message" (childEvent)="getMessage($event)"></app-communicating-components>

app-component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'AngularLesson2';

  public message = 'Hello my child, Parents here';
  public show: string;


  getMessage($event) {
    this.show = $event;
  }
}

communication-components.component.html

<p>
  {{Parent}}
</p>
<button (click)="fireEvent()">Fire from child</button>

communication-components.component.ts

import { Component, OnInit, Input, Output } from '@angular/core';
import { EventEmitter } from 'events';

@Component({
  selector: 'app-communicating-components',
  templateUrl: './communicating-components.component.html',
  styleUrls: ['./communicating-components.component.css']
})
export class CommunicatingComponentsComponent implements OnInit {

  @Input() Parent;
  @Output() public childEvent = new EventEmitter();


  constructor() {
  }

  ngOnInit() {

  }

  fireEvent() {
    this.childEvent.emit('Hi, I am child');
    console.log("event fire" + this.childEvent);
  }

}

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

我相信问题出在您的导入Array ( [WHERE] => Array ( [1] => Array ( [column] => status [op] => != [value] => 2 ) [2] => Array ( [column] => category_id [op] => = [value] => 3 ) ) [ORDER BY] => Array ( [1] => Array ( [by] => views [order] => ASC ) [2] => Array ( [by] => date [order] => DESC ) ) [LIMIT] => Array ( [1] => Array ( [offset] => 0 [limit] => 12 ) ) ) 上,而不是您应该在此处导入的问题。尝试将导入更改为WHERE status != 2 AND category_id = 3 ORDER BY views ASC, date DESC LIMIT 0, 12

更新

这是一个stackblitz example,表明它有效

答案 1 :(得分:0)

使用Angular较新的版本(例如8),您必须:

  1. 以k.s正确导入Emitter。说过: import { EventEmitter } from '@angular/core'
  2. 初始化输出的发射器: @Output() emisor = new EventEmitter<type>();