Angular动态表单项目迁移-'input'没有初始化程序和构造函数,TypeT|undefined的另一个错误
创始人
2024-10-24 14:01:13
0

这个问题通常发生在将 Angular 旧版代码升级到新版的过程中。原因是在旧版中,可能会在构造函数中初始化值,但在新版中,它会优先使用使用输入属性进行初始化,然后才会执行构造函数。解决方法是将输入属性值初始化放在构造函数之前,在组件中添加 ngAfterViewInit() 生命周期钩子方法。以下是示例代码:

import { AfterViewInit, Component, Input } from '@angular/core';
import { FormGroup } from '@angular/forms';

@Component({
  selector: 'app-dynamic-form',
  template: `
    
`, }) export class DynamicFormComponent implements AfterViewInit { @Input() fields: any[]; form: FormGroup; ngAfterViewInit() { // Initialize input values before the constructor is executed this.fields = this.fields || []; // Initialize the form after the input values this.form = this.createFormGroup(); } private createFormGroup() { const group = new FormGroup({}); this.fields.forEach((field) => { const control = field.required ? new FormControl(field.value || '', Validators.required) : new FormControl(field.value || ''); group.addControl(field.name, control); }); return group; } }

相关内容

热门资讯

据权威媒体报道!wepoker... 据权威媒体报道!wepoker有透视功能吗,天酷辅助器,竟然是真的有辅助技巧(哔哩哔哩)1)天酷辅助...
值得注意的是!德普之星私人局辅... 值得注意的是!德普之星私人局辅助免费,赣牌圈破解器,其实有辅助攻略(哔哩哔哩)1)德普之星私人局辅助...
受玩家影响!hhpoker真能... 受玩家影响!hhpoker真能买到挂吗,潮汕来物局游戏源码,一直是真的有辅助脚本(哔哩哔哩)潮汕来物...
有玩家发现!pokermast... 有玩家发现!pokermaster辅助器,新漫游免费辅助器,一贯是有辅助插件(哔哩哔哩)1、上手简单...
刚刚!wepoker透视脚本免... 刚刚!wepoker透视脚本免费app,衢州都莱罗松辅助软件,竟然存在有辅助app(哔哩哔哩)所有人...
近期!wepoker私人定制透... 近期!wepoker私人定制透视,中至上饶打炸辅助器开挂,果然存在有辅助攻略(哔哩哔哩)一、中至上饶...
经调查!wpk有作必弊吗,广东... 经调查!wpk有作必弊吗,广东雀神智能插件,确实有辅助app(哔哩哔哩)1.广东雀神智能插件 选牌创...
网友热议!wejoker手机版... 网友热议!wejoker手机版透视脚本,摸一把跑得快有挂吗,竟然存在有辅助工具(哔哩哔哩)小薇(辅助...
这一现象值得深思!hhpoke... 这一现象值得深思!hhpoker有没有辅助,正版新九游辅助,总是有辅助工具(哔哩哔哩)这一现象值得深...
据玩家消息!聚星ai辅助工具收... 据玩家消息!聚星ai辅助工具收费多少,牛总管辅助方法,真是有辅助神器(哔哩哔哩)1、聚星ai辅助工具...