月detail.component.html
import ...
@Component({
template: `{{month?.id}} <app-month-date [month]="month"></app-month-date>`
})
export class MonthDetailComponent implements OnInit {
month: Month;
ngOnInit(): void {
this.route.params
.switchMap((params: Params) => this.monthService.getMonth(+params["id"]))
.subscribe(month => (this.month = month));
}
}
月date.component.html
<p>month-date works! {{month?.id}}</p>
月date.component.ts
import { Component, OnInit, Input } from "@angular/core";
import { Month } from "app/attendance/month";
@Component({
selector: "app-month-date",
...
})
export class MonthDateComponent implements OnInit {
@Input() month: Month;
ngOnInit() {
//undefined --- here
console.log(this.month);
}}
月?.id在month-detail.component.html
中正确显示,但月份未定义,app-month-date
中的代码为month-date.component.ts
。
也许没有得到ngOnInit的价值?
答案 0 :(得分:1)
您可以通过在父模板中包含* ngIf来确保在月份输入值发送到子组件之前未初始化子组件来解决此问题:
@Component({
template: `{{month?.id}} <app-month-date *ngIf="month" [month]="month"></app-month-date>`
})