我使用角6,我想添加一个响应表与排序,过滤器和分页。所以我选择了材料。
我正在尝试实现这里的官方文件中描述的基本示例。
我得到了一个错误:
未知错误:模板解析错误:
不能绑定到“dataSource”,因为它不是表的已知属性。(“
这是我的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>-->我检查了同样的问题这里,但它有角5!
我补充说,这些步骤与角7很好地工作,但我需要角6版本。
有什么办法可以解决吗?
发布于 2019-01-25 15:34:15
我终于发现了我的错误:
我的组件位于一个app/reusables/components/ws-list可重用的可重用模块下
我应该在reusables.module.tn而不是app.module.ts中导入@转角/材料。
我希望这能帮到一个人!
谢谢
发布于 2019-01-24 16:26:38
下面是该表的基本斯塔克布利茨演示。
以下是我能够重现的错误的一些可能原因:
不能绑定到“dataSource”,因为它不是表的已知属性。
发布于 2019-01-24 16:35:40
很可能忘记将MatTableModule导入到app.module.ts中。您还应该确保您已经通过命令行中的"@angular/material": "^7.2.2"或npm install将ng add添加到您的package.json中。
https://stackoverflow.com/questions/54350807
复制相似问题