typescript 错误:失败:模板解析错误:“mat-checkbox”不是已知元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50448085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:25:28  来源:igfitidea点击:

error: Failed: Template parse errors: 'mat-checkbox' is not a known element

angulartypescripttestingangular-material

提问by Sangwin Gawande

I create this code for testing my component.

我创建此代码用于测试我的组件。

I tried this code:

我试过这个代码:

describe('Component: AddAlarms', () => {
    let component: AddAlarmsFormComponent;
    let fixture: ComponentFixture<AddAlarmsFormComponent>;
    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [AddAlarmsFormComponent]
        });
        fixture = TestBed.createComponent(AddAlarmsFormComponent);
        component = fixture.componentInstance;
    });
});

when run ng testshow this error:

运行时ng test显示此错误:

Failed: Template parse errors:
'mat-checkbox' is not a known element:
1. If 'mat-checkbox' is an Angular component, then verify that it is part of this module.
2. If 'mat-checkbox' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
        </div>
        <div class="input-field col s2">
          [ERROR ->]<mat-checkbox class="example-margin" (click)="sortbydate()">Dates</mat-checkbox>
        </div>
     "): ng:///DynamicTestModule/NotificationsComponent.html@12:10

I verify my module.ts and it's ok. So, I have this:

我验证了我的 module.ts 并且没问题。所以,我有这个:

import {MatCheckboxModule} from '@angular/material/checkbox';

Can you tell me, what is the problem?

你能告诉我,有什么问题吗?

回答by Sangwin Gawande

You need to add importsarray above declarationslike this :

您需要像这样在imports上面添加数组declarations

Add it like this :

像这样添加:

import { MatCheckboxModule } from '@angular/material/checkbox';

And add importsarray like this :

并添加这样的imports数组:

TestBed.configureTestingModule({
   imports: [
        MatCheckboxModule
   ],
   declarations: [AddAlarmsFormComponent]
})

回答by Chiien

Great!

伟大的!

But I think you need to import MatCheckboxModulein a spec.tsor you forgot to import the module in "imports"on ngModule!

但是我认为您需要导入MatCheckboxModuleaspec.ts或者您忘记将模块导入"imports"on ngModule

Can you try and tell me if worked?

你能试着告诉我是否有效吗?