角度:错误:“ mat-form-field”不是已知元素

时间:2020-08-10 07:25:34

标签: angular typescript

执行单元测试时,出现如下错误:

错误:“ mat-form-field”不是已知元素

错误:“ mat-grid-tile”不是已知元素

通常,app.module.ts中缺少导入时会出现以下错误,但我确定它们可用:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { NavigationviewComponent } from './navigationview/navigationview.component';
import { MainviewComponent } from './mainview/mainview.component';
import { DetailviewComponent } from './detailview/detailview.component';
import { TodolistComponent } from './todolist/todolist.component';
import { ListitemComponent } from './listitem/listitem.component';
import { ListitemdetailComponent } from './listitemdetail/listitemdetail.component';

import { DonePipe } from './DonePipe';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatNativeDateModule } from '@angular/material/core';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatButtonModule} from '@angular/material/button';
import {MatIconModule} from '@angular/material/icon';
import {MatBadgeModule} from '@angular/material/badge';
import {MatGridListModule} from '@angular/material/grid-list';
import {MatSnackBarModule} from '@angular/material/snack-bar';

import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { Configuration } from 'msal';
import {
  MsalModule,
  MsalInterceptor,
  MSAL_CONFIG,
  MSAL_CONFIG_ANGULAR,
  MsalService,
  MsalAngularConfiguration
} from '@azure/msal-angular';

import { isIE, msalAngularConfig, msalConfig } from './app-config';

function MSALConfigFactory(): Configuration {
  return msalConfig;
}

function MSALAngularConfigFactory(): MsalAngularConfiguration {
  return msalAngularConfig;
}

@NgModule({
  declarations: [
    AppComponent,
    NavigationviewComponent,
    MainviewComponent,
    DetailviewComponent,
    TodolistComponent,
    ListitemComponent,
    ListitemdetailComponent,
    DonePipe
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    BrowserAnimationsModule,
    FormsModule,
    CommonModule,
    MsalModule,
    MatDatepickerModule,
    MatNativeDateModule,
    MatInputModule,
    MatFormFieldModule,
    MatButtonModule,
    MatIconModule,
    MatBadgeModule,
    MatGridListModule,
    MatSnackBarModule
  ],
  exports: [
    MatDatepickerModule,
    MatNativeDateModule,
    MatInputModule,
    MatFormFieldModule,
    MatButtonModule,
    MatIconModule,
    MatBadgeModule,
    MatGridListModule,
    MatSnackBarModule
  ],
  providers: [
    MatFormFieldModule,
    MatDatepickerModule,
    MatNativeDateModule,
    MatButtonModule,
    {
      provide: HTTP_INTERCEPTORS,
      useClass: MsalInterceptor,
      multi: true
    },
    {
      provide: MSAL_CONFIG,
      useFactory: MSALConfigFactory
    },
    {
      provide: MSAL_CONFIG_ANGULAR,
      useFactory: MSALAngularConfigFactory
    },
    MsalService
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

还有什么需要考虑的,这样错误才不会出现吗?

预先感谢

2 个答案:

答案 0 :(得分:1)

在单元测试文件中,您必须导入MatFormFieldModule & MatGridListModul e

答案 1 :(得分:1)

尝试在MatFormFieldModule, MatGridListModule文件中导入.spec,以告知编译器Mat Form和Grid元素。

编辑: 根据菲利普的建议:)

beforeEach(() => {
  TestBed.configureTestingModule({
  imports: [MatFormFieldModule, MatGridListModule], // <== need to import both modules.
    providers: [...],
    declarations: [...],
 });
});