Formly v7自定义元素注册报错求助:类型‘button’未找到
Formly v7自定义组件注册报错的排查与修复
针对你遇到的"The type 'button' could not be found. Please make sure that is registered through the FormlyModule declaration."报错,按以下步骤排查:
检查Formly注册配置格式
确认FormlyModule.forRoot的types数组里,自定义组件的配置格式正确,必须包含name和component两个属性:@NgModule({ imports: [ BrowserModule, ReactiveFormsModule, FormlyModule.forRoot({ types: [ // 正确配置自定义button组件 { name: 'button', component: FormlyButtonComponent }, ], }), ], declarations: [FormlyButtonComponent], }) export class AppModule { }确认组件声明范围
自定义的FormlyButtonComponent必须在当前模块的declarations数组中;如果组件属于子模块,需要在子模块中声明并导出,同时在子模块中使用FormlyModule.forChild配置types:// 子模块示例 @NgModule({ declarations: [FormlyButtonComponent], exports: [FormlyButtonComponent], imports: [ FormlyModule.forChild({ types: [{ name: 'button', component: FormlyButtonComponent }], }), ], }) export class ButtonModule { }核对模板中type名称的一致性
在Formly字段配置里,type的值必须和注册时的name完全匹配(区分大小写):fields: FormlyFieldConfig[] = [ { type: 'button', // 必须和注册的name一致 templateOptions: { text: '提交', }, }, ];排查模块导入顺序
若同时使用FormlyModule.forRoot和FormlyModule.forChild,确保根模块的forRoot先导入,子模块的forChild后导入,避免配置被覆盖。检查名称冲突
确认没有其他第三方扩展或自定义组件注册了同名的button类型,避免名称冲突导致注册失效。
内容的提问来源于stack exchange,提问作者Mainak
相关产品推荐
相关产品推荐

