【发布时间】:2018-09-17 22:18:29
【问题描述】:
我有一个约定对象,我必须将其作为输入传递给子组件,当我在子模板中调用它时会显示约定 id,但是当我尝试在控制台中使用约定时会出现未定义。日志。 我尝试将我的console.log 放在孩子的构造函数中以及ngOnInit、ngAfterViewInit 和ngOnChanges 中。对于 ngOnChanges,当我只将约定的 id 作为输入传递给子组件时,它工作得很好,但我需要传递约定而不是它的 id。
index.component.ts
@Component({
templateUrl: './index.template.html',
selector: 'be-convention-update',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ConventionUpdateComponent {
private convention$: Convention;
private conventionId: number;
constructor(private route: ActivatedRoute,
private conventionService: ConventionService) {
this.route.params.subscribe(
(params: any) => {
this.conventionId = params['id'];
this.conventionService.getOne(this.conventionId).subscribe(response => this.convention$ = response);
}
);
}
}
index.template.html
<be-form-convention [convention]="convention$"></be-form-convention>
form.component.ts
@Component({
selector: 'be-form-convention',
templateUrl: './form.template.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ConventionFormComponent implements OnChanges, OnInit {
@Input() convention: Convention;
constructor() {
}
public ngAfterViewInit() {
console.log(this.convention); //undefined
}
public ngOnInit(): void {
console.log(this.convention); //undefined
}
public ngOnChanges(changes: SimpleChanges): void {
const object: SimpleChange = changes.convention;
console.log('prev value: ', object.previousValue); //undefined
console.log('got name: ', object.currentValue); //undefined
}
}
【问题讨论】:
-
你真的从你的conventionService.getOne那里得到你的约定对象吗?
-
$后缀尖叫着可观察到并且“需要异步管道”,但至少不清楚你在做什么以及为什么这样做。 -
@Orodan 是的,我从我的服务中获取约定对象
-
@AluanHaddad 你是对的,但这与我的问题无关
-
从我在这里看到的,你做得对,你能展示你的 ConventionUpdateComponent 模板和 Convention 模型吗?
标签: angular typescript