关于打字稿:角垫分隔线不是已知元素

Angular mat-divider is not a known element

我正在尝试以角度测试弹出组件,但我不知道为什么在启动测试时出现错误消息:

'mat-divider' is not a known element:
1. If 'mat-divider' is an Angular component, then verify that it is part of this module.
2. If 'mat-divider' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component
to suppress this message.

我在@NgModule中导入了它,这是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@NgModule({
  declarations: [
    AppComponent,
    AdminTopMenuComponent,
    SvGameCardComponent,
    SvCreationPopupComponent,
    MockPopupComponent,
\tMyDialogComponent,
\tFvCreationPopupComponent,
    GameModesComponent,
    LinkTestComponent,
    UserComponent,
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    AppRoutingModule,
    FormsModule,
    ReactiveFormsModule,
    MatDialogModule,
\tBrowserAnimationsModule,
\tMatButtonModule,
\tMatCheckboxModule,
    MatTableModule,
    MatDividerModule,
    MatInputModule,
    MatSelectModule,
    MatFormFieldModule,
    MatCardModule,
    AppRoutingModule,
    RouterModule,
  ],
  exports: [
      MatDividerModule,
      MatFormFieldModule,
  ],
  providers: [BasicService],
  bootstrap: [AppComponent],
  entryComponents: [
    MyDialogComponent,
    SvCreationPopupComponent,
    FvCreationPopupComponent,
    ]
})

export class AppModule {

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Component({
  selector: 'app-fv-creation-popup',
  templateUrl: './fv-creation-popup.component.html',
  styleUrls: ['./fv-creation-popup.component.css']
})
export class FvCreationPopupComponent implements OnInit {

  constructor(
    public dialogRef: MatDialogRef<FvCreationPopupComponent>, // dialogRef is now a reference to the diaolog popup
    @Inject(MAT_DIALOG_DATA) public data: any) { }      // allows the sharing of data through dialogConfig.data

  ngOnInit() {
   
  }
 
  submit(): void {
    //TODO: implementation de la fonction
    console.log("Submit fv-gameCard not implemented")
  }

  close(): void {
    this.dialogRef.close();
  }
  gameType: string[] = ["Formes géométriques","Thématique"];
 
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { /*async,*/ async, ComponentFixture, TestBed } from"@angular/core/testing";

import { MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from"@angular/material";
import { FvCreationPopupComponent } from"./fv-creation-popup.component";

describe("FvCreationPopupComponent", () => {
    let component: FvCreationPopupComponent;
    let fixture: ComponentFixture<FvCreationPopupComponent>;
    // const mock: MatDialogRef<FvCreationPopupComponent> = new MatDialogRef<FvCreationPopupComponent>(null, null) ;

    beforeEach(async(() => {
        // const data: MyDialogComponent = null;
        // data.message ="Dialog Message";
        // tslint:disable-next-line:typedef
        const mockDialogRef = {
            close: jasmine.createSpy("close"),
        };

        TestBed.configureTestingModule({
        imports: [MatDialogModule],
        declarations: [ FvCreationPopupComponent ],
        providers: [{ provide: MatDialogRef, useValue: {mockDialogRef} }, { provide: MAT_DIALOG_DATA, useValue: {} } ]})
        .compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(FvCreationPopupComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

    it("should create", () => {
        expect(component).toBeTruthy();
    });

});


我认为您必须在测试模块中包含MatDividerModule,您还必须对组件中使用的其他模块执行相同的操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from"@angular/material";
import { FvCreationPopupComponent } from"./fv-creation-popup.component";

describe("FvCreationPopupComponent", () => {
    let component: FvCreationPopupComponent;
    let fixture: ComponentFixture<FvCreationPopupComponent>;
    // const mock: MatDialogRef<FvCreationPopupComponent> = new MatDialogRef<FvCreationPopupComponent>(null, null) ;

    beforeEach(async(() => {
        // const data: MyDialogComponent = null;
        // data.message ="Dialog Message";
        // tslint:disable-next-line:typedef
        const mockDialogRef = {
            close: jasmine.createSpy("close"),
        };

        TestBed.configureTestingModule({
        imports: [MatDialogModule, MatDividerModule, ...],
        declarations: [ FvCreationPopupComponent ],
        providers: [{ provide: MatDialogRef, useValue: {mockDialogRef} }, { provide: MAT_DIALOG_DATA, useValue: {} } ]})
        .compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(FvCreationPopupComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
    });

    it("should create", () => {
        expect(component).toBeTruthy();
    });

});