相关疑难解决方法(0)

Angular 2 Testing - 异步函数调用 - 何时使用

在Angular 2中进行测试时,何时在TestBed中使用异步函数?

你什么时候用的?

 beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [MyModule],
            schemas: [NO_ERRORS_SCHEMA],
        });
    });
Run Code Online (Sandbox Code Playgroud)

你何时使用它?

beforeEach(async(() => {
    TestBed.configureTestingModule({
        declarations: [MyModule],
        schemas: [NO_ERRORS_SCHEMA],
    });
}));
Run Code Online (Sandbox Code Playgroud)

任何人都可以启发我吗?

unit-testing karma-jasmine angular2-testing angular angular-test

74
推荐指数
2
解决办法
3万
查看次数

MatSort 和 MatPaginator 在没有 setTimeOut 的情况下不起作用

我有一个来自端点的数据并将其放入 MatTableDataSource。我能够让它为 MatSort 和 MatPaginator 工作,但需要使用 setTimeOut,这似乎不是执行此操作的正确方法。如果我删除它,它会抱怨“无法读取未定义类型的属性”,我认为这是因为它尚未初始化。

我也试过:

  • 将其移动到afterviewinit,但数据是在afterviewinit被调用后加载的,所以它仍然不起作用
  • 在 this.datasource = new 之后使用 this.changeDetectorRef.detectChanges() ... 也不起作用

这是我当前的代码(正在运行,但使用 settimeout)

<div *ngIf="!isLoading">
    <div *ngFor="let record of renderedData | async" matSort>

    // ... some html to show the 'record'

    <mat-paginator #paginator
        [pageSizeOptions]="[5, 10, 20]">
    </mat-paginator>
</div>
Run Code Online (Sandbox Code Playgroud)

组件

export class ResultsComponent implements OnInit, OnDestroy, AfterViewInit {
    dataSource: MatTableDataSource<any> = new MatTableDataSource();
    renderedData: Observable<any>;

    @ViewChild(MatPaginator) paginator: MatPaginator;
    @ViewChild(MatSort) sort: MatSort;

    constructor(some service) {}

    ngOnInit() {
        const accountId = someOtherService.getAccountId();
        this.someService.getData(accountId)
            .subscribe((myData) => {
                    this.dataSource …
Run Code Online (Sandbox Code Playgroud)

javascript sorting pagination angular-material angular

4
推荐指数
2
解决办法
2196
查看次数