angular - 从父级到子级的角度传递数据是未定义的
问题描述
我正在尝试使用组件交互将数据从父级传递给子级
在父组件中:
<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
解决方案
如果要传递countTitle
文本,只需将其包装成''
. 没有''
它被认为是组件中的一个属性undefined
,所以为什么要进入undefined
子组件。
<app-news [leonardType]="'countTitle'"></app-news>
或者只是没有[]
<app-news leonardType="countTitle"></app-news>
推荐阅读
- persistence - 是否可以在 Google Colaboratory 上运行桌面应用程序?
- r - Sys.getenv 找不到环境变量
- string - MATLAB:如何根据重复数字划分字符串
- python - 使用递归删除列表元素
- c - 从列表中接受用户的部分字符串输入
- javascript - facebook Share Dialog (iframe) 的一部分不可见,我无法移动它
- java - 为什么spring data jpa-java.lang.IllegalStateException:OneToMany关系中同一实体的多个表示?
- python - 你能从python中的项目名称的列表中找到一个项目吗?
- python - ImportError:无法从“celery.bin.base”导入名称“命令”
- .net - 我可以使用 Mesibo API 和 .NET 技术构建应用程序吗?