简体   繁体   English

ng测试无法识别角形材料成分

[英]ng test wont recognize angular material components

Im tryng to test my application with NG TEST but the i use angular material in one of my components and they are not recognized as known elements in angular. 我正在尝试使用NG TEST测试我的应用程序,但是我在我的组件之一中使用了角形材料,因此它们不被认为是已知的角形元素。 How can i fix this? 我怎样才能解决这个问题?

the errors are like this : 错误是这样的:

'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>

Three things you should check: 您应该检查的三件事:

  1. if you already have imported MatFormFieldModule in you app.module 如果您已经在MatFormFieldModule中导入了app.module

  2. if it has been added to imports array in @NgModule , and also in TestBed.configureTestingModule({...}) because it's about test files xxx.component.spec.ts : imports:[MatFormFieldModule,...] 如果已将其添加到@NgModule以及TestBed.configureTestingModule({...}) imports数组中,因为它与测试文件xxx.component.spec.tsimports:[MatFormFieldModule,...]

  3. if you have imported CUSTOM_ELEMENTS_SCHEMA : 如果已导入CUSTOM_ELEMENTS_SCHEMA

    import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; in app.module.ts and xxxx.component.spec.ts , and if it has been added to @NgModule and TestBed.configureTestingModule({...}) schemas: [CUSTOM_ELEMENTS_SCHEMA] . app.module.tsxxxx.component.spec.ts ,以及是否已添加到@NgModuleTestBed.configureTestingModule({...}) schemas: [CUSTOM_ELEMENTS_SCHEMA]

For the last/third one, the error message is quite clear: 对于最后一个/第三个,错误消息非常清楚:

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

Angular Material Components can be used after importing the Module for the component in app.module.ts. 在app.module.ts中为组件导入模块后,可以使用Angular Material Components。 You can check the module to be imported in API Tab on official documentation - https://material.angular.io/components/form-field/api . 您可以在官方文档-https://material.angular.io/components/form-field/api的 API选项卡中检查要导入的模块。

The error will fixed for mat-form-field after importing the following module but you will also need to import the other modules. 导入以下模块后,该错误将针对mat-form-field修复,但您还需要导入其他模块。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM