You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 22:05:58