首页 > 解决方案 > 如何在 Angular 6 中实现材料表?

问题描述

我正在使用 Angular 6,我想添加一个带有排序、过滤和分页的响应表。所以我选择了材质。

我正在尝试实现此处官方文档中描述的基本示例。

我收到此错误:

未捕获的错误:模板解析错误:

无法绑定到“dataSource”,因为它不是“table”的已知属性。("

这是一个捕获

这是我的 app.module.ts

import { MatInputModule, MatPaginatorModule, MatProgressSpinnerModule, MatSortModule, MatTableModule } from '@angular/material';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
...
imports: [
BrowserModule,
BrowserAnimationsModule,
MatInputModule,
MatPaginatorModule,
MatProgressSpinnerModule,
MatSortModule,
MatTableModule,
...
]

这是我的 ws-list.component.ts

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
];


@Component({
  selector: 'app-ws-list',
  templateUrl: './ws-list.component.html',
  styleUrls: ['./ws-list.component.css']
})

export class WorkspacesListComponent implements OnInit {
  displayedColumns: string[] = ['position', 'name', 'weight',     'symbol'];
  dataSource = ELEMENT_DATA;

这是我的 ws-list.component.html:

  <table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

<!--- Note that these columns can be defined in any order.
      The actual rendered columns are set as a property on the row definition" -->

<!-- Position Column -->
<ng-container matColumnDef="position">
  <th mat-header-cell *matHeaderCellDef> No. </th>
  <td mat-cell *matCellDef="let element"> {{element.position}} </td>
</ng-container>

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

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

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

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

我在这里检查了同样的问题,但它与Angular 5有关!

我补充说这些步骤适用于 Angular 7。但我需要 Angular 6 版本。

有什么想法可以解决吗?

标签: angularangular-materialangular-cli-v6

解决方案


我终于发现了我的错误:

我的组件位于可重用模块下:app/reusables/components/ws-list

我应该在reusables.module.tn而不是app.module.ts中导入 @angular/material

我希望这可以帮助一些人!

谢谢


推荐阅读