我正在测试一个使用 mat-table
的 Angular 7 组件,我已经定义了一个 mock 模块,它声明并导出一个 mock mat-row
组件:
@Component({selector: 'mat-row', template: ''}) export class StubMatRowComponent {
@Input() public matRowDefColumns: string[];
}
模板中的用法是这样的:
<mat-row *matRowDef="let foo; columns: displayedColumns;"></mat-row>
使用此语法,我得到的错误是“无法绑定(bind)到‘matRowDefColumns’,因为它不是‘mat-row’的已知属性。”
如果我不需要foo
,我可以这样写,这会起作用:
<mat-row matRowDef="displayedColumns"></mat-row>
有谁知道如何使用模板语法让模拟工作?
作为旁注,很明显,除了导入我的模拟模块之外,我还可以导入真实的模块,但同样明显的是,出于测试目的,我更喜欢模拟而不是完全集成。
最佳答案
答案是因为 matRowDef
是一个结构指令,而不是要绑定(bind)的属性,所以必须创建并导入模拟结构指令:
@Directive({
selector: '[matRowDef]'
}) export class StubMatRowDefDirective {
@Input() matRowDefColumns: string[];
}
关于angular - 无法绑定(bind)到 'matRowDefColumns',因为它不是 'mat-row' 的已知属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58830187/