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、打开软...
现有关情况通报如下!购买的wp... 您好,购买的wpk辅助在哪里下载这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
透视透视挂!xpoker辅助(... 透视透视挂!xpoker辅助(透视)竟然有辅助下载(有挂总结)-哔哩哔哩1、该软件可以轻松地帮助玩家...
透视代打!wepoker是不是... 透视代打!wepoker是不是有人用挂(透视)切实真的有辅助插件(有挂透明挂)-哔哩哔哩1、点击下载...
据监测!hhpoker德州透视... 据监测!hhpoker德州透视挂(透视)本来是真的辅助插件(有挂猫腻)-哔哩哔哩hhpoker德州透...
透视游戏!wepoker怎么挂... 透视游戏!wepoker怎么挂底牌(透视)都是有辅助app(真实有挂)-哔哩哔哩1、wepoker怎...
透视黑科技!newpoker怎... 透视黑科技!newpoker怎么安装脚本(透视)切实真的有辅助神器(有挂细节)-哔哩哔哩newpok...
透视中牌率!德普之星透视辅助软... 透视中牌率!德普之星透视辅助软件下载(透视)果然真的是有辅助工具(证实有挂)-哔哩哔哩1、完成德普之...
今日!wepoker私人局透视... 今日!wepoker私人局透视教程(透视)好像真的有辅助软件(有挂实锤)-哔哩哔哩1、每一步都需要思...
透视游戏!wepoker透视功... 透视游戏!wepoker透视功能下载(透视)总是真的是有辅助安装(有挂工具)-哔哩哔哩1、wepok...