ng test不会识别角度材料成分

时间:2018-04-18 01:17:07

标签: angular angular-material

我尝试使用NG TEST测试我的应用程序,但我在我的一个组件中使用角度材料,并且它们不被识别为角度中的已知元素。我该如何解决这个问题?

错误是这样的:

'mat-form-field' is not a known element:
1. If 'mat-form-field' is an Angular component, then verify that it is part of this module.
2. If 'mat-form-field' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
<form class="example-form">
    [ERROR ->]<mat-form-field class="example-full-width">
      <input matInput placeholder="Usuário" [formControl"): ng:///DynamicTestModule/TableComponent.html@2:4
Can't bind to 'dataSource' since it isn't a known property of 'mat-table'.
1. If 'mat-table' is an Angular component and it has 'dataSource' input, then verify that it is part of this module.
2. If 'mat-table' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
  </form>

2 个答案:

答案 0 :(得分:3)

你应该检查三件事:

  1. 如果您已经导入 MatFormFieldModule app.module

  2. 如果已将其添加到 imports 中的 @NgModule 数组中,也添加到TestBed.configureTestingModule({...})中,因为它&# 39;关于测试文件xxx.component.spec.tsimports:[MatFormFieldModule,...]

  3. 如果您已导入 CUSTOM_ELEMENTS_SCHEMA

    import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';app.module.ts中的

    xxxx.component.spec.ts,如果已添加到 @NgModule TestBed.configureTestingModule({...}) < / strong> schemas: [CUSTOM_ELEMENTS_SCHEMA]

  4. 对于最后一个/第三个,错误信息非常清楚:

    To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("</form>.....

答案 1 :(得分:0)

在app.module.ts中导入组件的模块后,可以使用Angular Material Components。您可以在官方文档https://material.angular.io/components/form-field/api上查看要在API标签中导入的模块。

导入以下模块后,mat-form-field的错误将被修复,但您还需要导入其他模块。

import {MatFormFieldModule} from '@angular/material/form-field';