通常,当在Angular项目中使用Material模块时,会出现如下错误:“'mat-form-field' is not a known element...”,这通常是因为在单独运行测试文件时缺少Material模块的导入,因此需要在测试文件中添加Material模块的导入语句,例如:
import { MatFormFieldModule } from '@angular/material';
...
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ MatFormFieldModule ],
...
}).compileComponents();
}));
在这个示例中,我们在测试文件中导入了MatFormFieldModule模块,并将其添加到TestBed的imports中,以解决单元测试失败的问题。