我刚开始使用@Input
Angular 的功能,到目前为止一切都很好。我使用 Firebase 作为我的数据库,我调用的数据块看起来像这样
{
"page_area_business_image" : {
"expand" : {
"intro" : "some info...",
"title" : "a title"
},
"rebrand" : {....},
"newbrand" : {....},
"questions" : {
"question01" : {
"id" : "an ID",
"name" : "a name",
"question" : "a question",
"answers" : {
"answer01" : {
"answer" : "some answer",
"id" : "an ID"
},
"answer02" : {
"answer" : "another answer",
"id" : "an ID"
}
}
},
"question02" : {....},
"question03" : {....}
}
}
}
我现在唯一遇到的问题是让子组件读取父组件中完全正常的questions
数据console.log()
,这意味着服务文件正在传递它。我是这样称呼它的。
父组件
private busExpInformation: FirebaseObjectObservable<any>;
private busImageO: FirebaseObjectObservable<any>;
private busImageQuesO: FirebaseObjectObservable<any>;
constructor(private _homeService: HomeService) { }
ngOnInit() {
this._homeService.getBusExpInformation().subscribe(BusExpInformation =>{
this.busExpInformation = BusExpInformation;
});
this._homeService.getBusImage().subscribe(BusImage => {
this.busImageO = BusImage.expand;
this.busImageQuesO = BusImage.questions;
console.log(this.busImageQuesO); //works fine here
});
}
父模板
<business-image [busImageI]="busImageO" [busImageQuesI]="busImageQuesO"></business-image>
子组件
@Input('busImageI')
busImage: FirebaseObjectObservable<any>;
@Input('busImageQuesI')
questions: FirebaseObjectObservable<any>;
ngOnInit() {
console.log(this.questions);// doesn't return anything
}
我什至回到父组件并像这样在它自己对数据库的调用中调用它
this._homeService.getBusImage().subscribe(BusImage => {
this.busImageQuesO = BusImage.questions;
console.log(this.busImageQuesO);
});
它仍然没有对子组件产生影响。通过我一直在研究的其他元素,另一个和其他所有元素都显示得很好。
我在我遇到的一些文章和教程中看到他们强调在父组件中导入和声明子组件,但我已经看到其他他们只是在主模块中声明父组件的地方组件链接到,这就是我设置它的方式。我不知道这是否有所作为。我见过导入多个@Input
s 的其他示例,但我看不出我在做什么不同。