首页 > 解决方案 > 异步管道传递的角度输入数据在子组件中为空,但在父组件中不为空

问题描述

我有一个奇怪的案例,我无法通过并且不知道它是如何发生的,所以我在SiteComponent 这里命名了父组件打字稿逻辑:

ngOnInit(): void {
 this.subs.push(
  this.route.data.subscribe(data => {
    this.siteTemplate$ = this.dataService.getMappedData(`data${data.type}`).pipe(
      map(_data => ({..._data, dataType: data.type })),
      )
   })
 );
}

这里是模板文件代码:

<div class="app-site">
 <ng-container *ngIf="!siteTemplate$ | async">
    <div fxLayout="row" 
         fxLayoutAlign="center"
         style="margin-top: 60px;">
        <mat-spinner></mat-spinner>
    </div>
 </ng-container>
 <ng-container *ngIf="siteTemplate$ | async">
    <app-filters (filtersChanged)="applyFilters()"></app-filters>
    <div class="app-site-section">
        <div class="app-site-section-column" id="charts">
            <app-linear-chart *ngFor="let record of (siteTemplate$ | async)?.chartsData.records" 
                              [categories]="(siteTemplate$ | async)?.chartsData.categories" 
                              [chartData]="record"
            ></app-linear-chart>
        </div>
        <div class="app-site-section-column">
            <app-pie-chart *ngFor="let record of (siteTemplate$ | async)?.chartsData.records" 
                           [categories]="(siteTemplate$ | async)?.chartsData.categories"
                           [chartData]="record"
            ></app-pie-chart>
        </div>
    </div>
    <div>{{siteTemplate$ | async | json}}</div>
    <div fxLayout="row"
        fxLayoutAlign="center"
        id="table"
        class="app-site-section"
    >
        <app-table [tableData]="(siteTemplate$ | async)?.tableData" 
                   [dataType]="(siteTemplate$ | async)?.dataType"
        ></app-table>
    </div>
 </ng-container>
</div>

如您所见,它由几个子组件组成,这些子组件依赖于异步管道,图表工作完美,但表格存在问题。

table.ts

export class TableComponent implements AfterViewInit, OnInit {
  @Input() tableData: any = {records: []};
  @Input() dataType: any;

  dataSource: MatTableDataSource<any> ;

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

  constructor() {
  }

  ngOnInit(): void {
    console.log(this.tableData);

    this.dataSource = new MatTableDataSource(this.tableData.records);
  }

  ngAfterViewInit(): void {

    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }
}

在这里我收到一个错误,因为console.log(this.tableData)返回 null,但是 tableData 永远不会为 null,siteTemplate$因为我通过添加 tap 并记录这个值来检查它,它总是显示为对象。

标签: angulartypescriptmat-tableasync-pipe

解决方案


您有多个siteTemplate$ | async,每个都将订阅可观察的,所以siteTemplate$ | async可以null. 为了防止它,siteTemplate$ | async只使用一次:

<ng-container *ngIf="siteTemplate$ | async as siteTemplate; else other">
   ...
   <app-table [tableData]="siteTemplate.tableData" 
                   [dataType]="siteTemplate.dataType"
        ></app-table>
</ng-container>
<ng-template #other>
    <div fxLayout="row" 
         fxLayoutAlign="center"
         style="margin-top: 60px;">
        <mat-spinner></mat-spinner>
    </div>
 </ng-template>

推荐阅读