angular - 如何在 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 版本。
有什么想法可以解决吗?
解决方案
我终于发现了我的错误:
我的组件位于可重用模块下:app/reusables/components/ws-list
我应该在reusables.module.tn而不是app.module.ts中导入 @angular/material
我希望这可以帮助一些人!
谢谢
推荐阅读
- html - 在透明导航栏下方显示网站背景
- centos - 当我查看 freeswitch 日志时,会看到以下错误
- go - 反向绑定的缺失样本申请
- c# - 如何在 WCF 应用程序中配置应用程序端以与预配置的 ADFS 服务器进行 SSL 连接?
- c++ - 如何同步多个 esp8266?
- terraform - 如何在 terraform 中回滚到以前的状态
- variables - 使用concat()动态调用参数,选择元素名称
- spring-boot-admin - Spring Boot Admin:自定义标头身份验证
- mongodb - 如何修复“无法识别的表达式 $round”,同时使用 mongodb $and 条件和 $lookup 将其应用于管道中的存储数据
- sql-server - 将动态参数传递给 SQL