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

时间:2018-05-21 11:40:20

标签: angular typescript testing angular-material

我创建了这个代码来测试我的组件。

我试过这段代码:

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

运行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

我验证了我的module.ts,没关系。所以,我有这个:

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

你能问我,问题是什么?

2 个答案:

答案 0 :(得分:12)

您需要在imports之上添加declarations数组,如下所示:

像这样添加:

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

并像这样添加imports数组:

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

答案 1 :(得分:0)

大!

但我认为您需要在$curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://admin-official.line.me/11702069/home/api/objects", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => array( "csrf_token" => $split2[0], "file" => new CURLFile("test.jpg") ), //CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"csrf_token\"\r\n\r\nETukCyg_-OM72cZ0MuAyunvvy4cb8Cx2QqoP41T7\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", CURLOPT_HTTPHEADER => array( "Accept: */*", "Accept-Encoding: gzip, deflate, br", "Accept-Language: en-US,en;q=0.9,id;q=0.8,de-DE;q=0.7,de;q=0.6", "Cache-Control: no-cache", "Connection: keep-alive", "Content-Type: multipart/form-data", "Cookie: tzoffset=-420; _trmccid=6b030cba93d9db09; _ga=GA1.2.1058855825.1526457491; _gid=GA1.2.814129237.1526457491; ldsuid=CiDum1r75zWBJ3f7oLqRAg==; _trmcdisabled2=-1; _trmcuser=^{^\^id^^:^\^^^^}; _ga=GA1.3.1058855825.1526457491; _gid=GA1.3.814129237.1526457491; plack_session_localaccount=COOKIE; minesota=COOKIE; minesota2=COOKIE; _trmcsession=^{^\^id^^:^\^36b6efe6^^,^\^path^^:^\^/11702069/home/send/^^,^\^query^^:^\^^^,^\^time^^:1526880852499,^\^_dice^^:0.4099096438552394^}; plack_session=519cce226206bd15cba86f2153c9dc91a56df4fb; _trmcpage=/11702069/home/send/; __try__=1526881572577", "Origin: https://admin-official.line.me", "Postman-Token: 8b9dc9c8-166a-4c50-93c4-b16ca72d72dc", "Referer: https://admin-official.line.me/11702069/home/send/", "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36", //"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" ), )); curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; } 中导入MatCheckboxModule,或者您忘记在spec.ts"imports"导入该模块!

你能告诉我是否有效?