子组件从父组件接收数据时出错

时间:2017-03-10 12:29:32

标签: angular typescript angular2-components angular2-inputs

我将数据传递给父母的孩子

`<tag-editor[item]="item"></tag-editor>`

这里的项目有数据

子组件:

export class TagEditorComponent implements  OnInit, OnChanges {
@Input() private item: Tag;
ngOnInit() {
  console.log(this.item)
}
ngOnChanges() {
  console.log(this.item)
}

但我只有2 undefined

我该如何解决?

1 个答案:

答案 0 :(得分:2)

//Try this : 
import {OnInit, SimpleChanges, OnChanges} from '@angular/core';
export class TagEditorComponent implements  OnInit, OnChanges {
@Input() private item: Tag;
ngOnInit() {
  console.log(this.item)
}
ngOnChanges(changes : SimpleChanges) {
  if(typeof changes['item'] !== 'undefined'){
  console.log(this.item)
  }
}