@Input 没有从父组件接收数据

@Input not receiving data from parent component

我刚刚开始使用 Angular 的 @Input 功能,到目前为止一切都很好。我使用 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 的示例,但我看不出我在做什么不同。

尝试在子组件中将ngOnInit()更改为ngOnChanges(),并且不要忘记通过导入OnChanges和实施它。有关更多信息,请查看 this.