【发布时间】:2019-01-24 16:05:12
【问题描述】:
我正在使用 Angular 6,我想添加一个具有排序、过滤和分页功能的响应式表格。所以我选择了 Material。
我正在尝试实现the official documentation here 中描述的基本示例。
我收到此错误:
未捕获的错误:模板解析错误:
无法绑定到“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>-->
我检查了同样的问题here,但它与 Angular 5 相关!
我补充说这些步骤适用于 Angular 7。但我需要 Angular 6 版本。
有什么办法解决这个问题吗?
【问题讨论】:
-
您可以编辑代码以共享您的代码吗?
-
@A.Sana 可以把你的代码放在
staclblitz -
您是否导入了合适的模块?
import {MatTableModule} from '@angular/material/table'; -
感谢您的 cmets!我添加了我的代码
标签: angular angular-material angular-cli-v6