首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular::mat-table -未解析变量

Angular::mat-table -未解析变量
EN

Stack Overflow用户
提问于 2020-04-23 22:09:41
回答 2查看 794关注 0票数 1

我得到了未解决的变量stationId和未解决的变量街异常+表是完全空的(即使没有头)。控制台没有任何错误。对数据服务器的请求成功。

表格html:

代码语言:javascript
复制
<table mat-table [dataSource]="stations" class="mat-elevation-z8">

  <ng-container matColumnDef="stationId">
    <th mat-header-cell *matHeaderCellDef>stationId</th>
    <td mat-cell *matCellDef="let element"> {{element.stationId}} </td>
  </ng-container>

  <ng-container matColumnDef="operator">
    <th mat-header-cell *matHeaderCellDef>operator</th>
    <td mat-cell *matCellDef="let element"> {{element.operator}} </td>
  </ng-container>

  <ng-container matColumnDef="city">
    <th mat-header-cell *matHeaderCellDef>city</th>
    <td mat-cell *matCellDef="let element"> {{element.city}} </td>
  </ng-container>

  <ng-container matColumnDef="address">
    <th mat-header-cell *matHeaderCellDef>address</th>
    <td mat-cell *matCellDef="let element"> {{element.street}} </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

表ts:

代码语言:javascript
复制
export class StationsComponent implements OnInit {
  title = "STK";
  stations: MatTableDataSource<StationsInterface>;
  paginator: MatPaginator;
  displayedColumns: ["stationId", "operator", "city", "address"];

  constructor(@Inject("StationsAPIService") private stationService: StationsAPIService) {
  }

  ngOnInit() {
    this.stationService.getStations().subscribe(stations => {
      this.stations = new MatTableDataSource(stations);
      this.stations.paginator = this.paginator;
      console.log(stations);
    });
  }

}

工作站接口:

代码语言:javascript
复制
export interface StationsInterface {
  stationId: number;
  scopeOfApproval: string;
  postalCode: string;
  city: string;
  street: string;
  operator: string;
  telephoneNumber: string;
  email: string;
  community: string;
  district: string;
  region: string;
}

当我使用不带mat-table和ng- dataSource的相同容器时,行呈现得很好。有谁能帮我修一下吗?提前谢谢你。

Upd.:我在尝试renderRows()时也遇到了这个错误:

代码语言:javascript
复制
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'dataSource: [object Object]'. Current value: 'dataSource: undefined
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-24 00:16:37

我想是因为你的displayedColumns没有定义

尝试

代码语言:javascript
复制
displayedColumns = ["stationId", "operator", "city", "address"];

而不是

代码语言:javascript
复制
displayedColumns: ["stationId", "operator", "city", "address"];
票数 0
EN

Stack Overflow用户

发布于 2020-04-23 22:29:27

尝试使用async管道:

代码语言:javascript
复制
<table mat-table [dataSource]="stations$ | async" class="mat-elevation-z8">
代码语言:javascript
复制
export class StationsComponent {
  title = "STK";
  stations: MatTableDataSource<StationsInterface>;
  paginator: MatPaginator;
  displayedColumns: ["stationId", "operator", "city", "address"];

  readonly stations$ = this.stationService.getStations().pipe(
    map((stations) => {
      const source =  new MatTableDataSource(stations);
      source.paginator = this.paginator;
    }),
    shareReplay({ refCount: true, bufferSize: 0 })
  );

  constructor(@Inject("StationsAPIService") private stationService: StationsAPIService) {
  }
}

我在模板中遗漏了paginator,你确定它在那里吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61389250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档