【问题标题】:'mat-form-field' is not a known element - Angular 4 & Angular Material 2'mat-form-field' 不是已知元素 - Angular 4 和 Angular Material 2
【发布时间】:2017-11-07 12:59:45
【问题描述】:

在我的 Angular 应用程序中,我有一个包含所有组件的 presentation.module。我创建了一个shared-material.module,其中包含整个应用程序中使用的所有角度材料 2 模块。我在之前的presentation.module中已经导入了。在我的app.module 中,我导入了presentation.module

app.module 在其声明中包含app.componentheader.componentfooter.component

这是我的app.module

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    FooterComponent
  ],
  imports: [
    // Layer's modules
    PresentationModule,
    BusinessDelegateModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

这是我的presentation.module

const modules = [
  SharedMaterialModule,
  SharedModule,
  HomePresentationModule,
  SecurityPresentationModule,
]

@NgModule({
  imports: [...modules],
  exports: [...modules]
})
export class PresentationModule {
}

shared-material.module的源码:

// updated: Before, i have just imported these modules without re-exporting them.
const materialModules = [
  MatButtonModule,
  MatMenuModule,
  MatToolbarModule,
  MatIconModule,
  MatCardModule,
  MatSidenavModule,
  MatFormFieldModule,
  MatInputModule,
  MatTooltipModule
];
@NgModule({
  imports: [...materialModules],
  exports: [...materialModules],
  declarations: []
})
export class SharedMaterialModule {
}

如下图所示,我注册了一个security-presentation.module,这里是它的源代码:

@NgModule({
  imports: [
    SharedModule,
    SecurityRoutingModule
  ],
  declarations: [
    LoginComponent,
    RegisterComponent,
    EmailConfirmationComponent,
    PasswordResetComponent
  ]
})
export class SecurityPresentationModule {
}

我的问题是当我尝试在 login.component (在 security-presentation.module 中)使用 mat-input-field 时,我收到了这个错误:

Uncaught Error: Template parse errors:
'mat-form-field' is not a known element

但其他角材料 2 组件在 app.componentheader.componentfooter.component 中工作正常:

app.component.html

<div>

  <app-header></app-header>

  <div class="main-container">
    <router-outlet></router-outlet>
  </div>

  <app-footer></app-footer>

</div>

我应该在每个演示模块中导入shared-material.module 还是有办法解决这个问题?

提前谢谢你。

【问题讨论】:

  • 我遇到了同样的问题,但尚未找到解决方案。 :(你能解决它吗?

标签: angular typescript module angular-material2


【解决方案1】:

因为你的共享模块应该首先导入材料模块:

const materialModules = [
    MatButtonModule,
    MatMenuModule,
    MatToolbarModule,
    MatIconModule,
    MatCardModule,
    MatSidenavModule,
    MatFormFieldModule,
    MatInputModule,
    MatTooltipModule
];
@NgModule({
  imports: [...materialModules],
  exports: [...materialModules],
  declarations: []
})
export class SharedMaterialModule {
}

【讨论】:

  • 我遇到了同样的错误,我已经添加了这个imports 属性。
  • 抱歉,您的代码中没有。你也把它放在你的演示模块中了吗?如果是这样,如果您可以将其添加到您的帖子中,那就太好了,因为您可以看到它确实会让人感到困惑。
  • 我更新了我的帖子。我不明白为什么有些材质模块在app.componentheader.componentfooter.component 中工作正常,即使我在共享模块中导入它们也不能在其他视图中工作。
  • 嗡嗡声:您当前的角度材料版本是什么? mat-form-field 是最新的,也许你不是最新的?尝试使用 md-form-field 来查看?其他组件是否在演示模块中起作用,或者它们都是错误的?
  • 我明白了。演示模块没有导入,是正常的还是你忘记了?
猜你喜欢
  • 2018-02-20
  • 2018-06-14
  • 1970-01-01
  • 1970-01-01
  • 2018-10-24
  • 2020-07-02
  • 2018-04-16
  • 2021-01-22
  • 2020-04-30
相关资源
最近更新 更多