Angular 错误类型错误:在尝试修补ngOnInit()中的值时,无法读取未定义的属性“form”
如何在ngOnInit函数中修补值,我尝试将值快照从链接修补到html选择,但它不起作用 注意: onTest当我尝试使用单独的功能时,它工作得非常好Angular 错误类型错误:在尝试修补ngOnInit()中的值时,无法读取未定义的属性“form”,angular,typescript,angular-reactive-forms,Angular,Typescript,Angular Reactive Forms,如何在ngOnInit函数中修补值,我尝试将值快照从链接修补到html选择,但它不起作用 注意: onTest当我尝试使用单独的功能时,它工作得非常好 @ViewChild('FomrElm',{static:false}) RentForm : NgForm; ngOnInit() { if( this.route.snapshot.params['brand'] != null ){ console.log('::'+this.route.snapshot.params['br
@ViewChild('FomrElm',{static:false}) RentForm : NgForm;
ngOnInit() {
if( this.route.snapshot.params['brand'] != null ){
console.log('::'+this.route.snapshot.params['brand']);
this.RentForm.form.patchValue({
Brand : 'Peugeot'
})
this.brand_V = this.route.snapshot.params['brand'] ;
this.model_V = this.route.snapshot.params['model'] ;
}
onTest(){
this.RentForm.form.patchValue({
Brand : 'Peugeot'
})
}
}
表单尚未在父组件的ngOnInit中初始化。您可以实现AfterViewInit并访问ViewChild的初始化表单 下面是一个stackblitz供您回顾
export class AppComponent implements OnInit, AfterViewInit {
@ViewChild(UserFormComponent)
public userForm: UserFormComponent;
ngOnInit() {
}
ngAfterViewInit() {
// The form in ViewChild will not be null.
console.log(this.userForm);
}
}
请参阅下面stackblitz中的控制台,以了解差异
同样的问题,当我提交正常是OK的,但当我得到参数的链接,有一个issue@ElAmSouhail我已经更新了stackblitz,将url参数修补到表单中。