【问题标题】:How can I implement a material table in Angular 6?如何在 Angular 6 中实现材料表?
【发布时间】:2019-01-24 16:05:12
【问题描述】:

我正在使用 Angular 6,我想添加一个具有排序、过滤和分页功能的响应式表格。所以我选择了 Material。

我正在尝试实现the official documentation here 中描述的基本示例。

我收到此错误:

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

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

here is a capture

这是我的 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>-->

我检查了同样的问题here,但它与 Angular 5 相关!

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

有什么办法解决这个问题吗?

【问题讨论】:

  • 您可以编辑代码以共享您的代码吗?
  • @A.Sana 可以把你的代码放在staclblitz
  • 您是否导入了合适的模块? import {MatTableModule} from '@angular/material/table';
  • 感谢您的 cmets!我添加了我的代码

标签: angular angular-material angular-cli-v6


【解决方案1】:

我终于发现了我的错误:

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

我应该在 reusables.module.tn 中而不是在 app.module.ts

中导入 @angular/material

希望对大家有所帮助!

谢谢

【讨论】:

    【解决方案2】:

    您很可能忘记将MatTableModule 导入到您的 app.module.ts 中。您还应该确保在命令行中通过ng addnpm install"@angular/material": "^7.2.2" 添加到您的package.json。

    【讨论】:

    • 感谢您的回答,但“@angular/material”:“^7.2.2”,在安装@angular/material 后会自动添加到我的package.json!
    【解决方案3】:

    这是一个基本的Stackblitz 演示表。

    以下是我能够重现的错误的一些可能原因:

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

    • 您没有安装 @angular/material 或其依赖项(npm install @angular/material @angular/cdk @angular/animations --save)
    • 您没有在应用模块中导入它(将其添加到导入数组中
    • 您尚未将 mat-table 指令添加到您的表中

    【讨论】:

    • 感谢您的回答,但我做到了。我安装了@angular/material 并导入了模块!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 2019-05-16
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多