【发布时间】:2018-07-31 09:21:59
【问题描述】:
我正在尝试使用组件交互将数据从父级传递给子级
在父组件中:
<app-news [leonardType]="countTitle"></app-news>
子组件
export class NewsComponent implements OnInit, AfterViewChecked {
@Input() leonardType: string;
title;
constructor(
public newsService: NewsService
) {}
ngAfterViewChecked() {
console.log(this.leonardType); ==> undefined
console.log(this.newsService.leonardCategory[this.leonardType]); ==> undefined
}
ngOnInit() {
this.title = this.newsService.leonardCategory[this.leonardType];
console.log(this.leonardType); ==> undefined
console.log(this.newsService.leonardCategory[this.leonardType]); ==> undefined
}
在子组件服务中我有一个对象:
leonardCategory = {
countTitle: "TITLE 1",
indTitle: "TITLE 2"
};
我认为该视图尚未呈现,但在 ngAfterViewChecked 中我得到了 leonardType 的未定义输出
【问题讨论】:
标签: angular typescript