ell*_*er7 8 testing angular-material angular-cli angular6
我有一个使用 Material Design 的 Angular6 应用程序。运行“ng test”时,出现以下错误:
失败:模板解析错误:无法绑定到“value”,因为它不是“mat-select”的已知属性。
我在导入和导出中包含了 MatSelectModule。它在应用程序中运行良好,但在测试期间失败。
material-design.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatFormFieldModule, MatInputModule, MatSelectModule} from '@angular/material';
@NgModule({
imports: [MatFormFieldModule, MatInputModule, MatSelectModule],
exports: [MatFormFieldModule, MatInputModule, MatSelectModule],
})
export class MaterialDesignModule { }
Run Code Online (Sandbox Code Playgroud)
选择示例:
<mat-form-field class='select_buttons'>
<mat-select (selectionChange)='typeSelection_changed($event)' [(value)]='type'>
<mat-option *ngFor="let type of types" [value]="type"> {{type.name}}</mat-option>
</mat-select>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
请按照以下步骤操作 -
import { MatSelectModule } from '@angular/material/select';在你的文件名.module.ts中
加入-MatSelectModuleimports
@NgModule({
declarations: [MyComponent],
imports: [
MatSelectModule,
]
})
Run Code Online (Sandbox Code Playgroud)
小智 4
您是否在 spec.ts 文件中导入材质模块?
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ MaterialDesignModule ],
declarations: [ FooComponent ]
})
.compileComponents();
}));
Run Code Online (Sandbox Code Playgroud)