无法访问NgOnInit -Angular 6中的输入属性

时间:2019-05-20 22:27:56

标签: angular

“我的父母”页面如下所示。

html:

<counter  [conversationId]="conversationId"></counter>

组件:

public conversationId: string;

public showModal() {   
 this.conversationId = '1234'
 this.modalService.showComponent(CounterPage, { class: 'modal-lg' }); 
}

“我的孩子”页面看起来像这样。

 @Input() conversationId: any;
 public ngOnInit  (): void {
    alert(this.conversationId); //shows undefinded.
  }

看起来这是常见的问题here。没有一种解决方案对我有用。

1 个答案:

答案 0 :(得分:0)

这些参数在ngAfterContentInit()方法之前接收值。

在此图像中查看角度的生命周期:https://v2.angular.io/resources/images/devguide/lifecycle-hooks/hooks-in-sequence.png

尝试:

@Input() conversationId: any;
 public ngAfterContentInit  (): void {
    alert(this.conversationId); //shows undefinded.
  }