【发布时间】:2019-08-12 00:34:28
【问题描述】:
我花了两天时间寻找所有可能的解决方案(见下文),并将 Angular 和 Material 从 7 升级到 8,仍然是同样的抱怨。
无法绑定到“dataSource”,因为它不是“table”的已知属性
-
import和@NgModule在 app.module.ts 和 component.ts 中 (也尝试了部分,所有可能的组合`) - 将
<table换成<mat-table -
[dataSource]不是[datasource]
app.module.ts
import { MatFormFieldModule, MatInputModule} from '@angular/material';
import { MatTableModule } from '@angular/material/table'; // or just @angular/material
@NgModule({
imports:
[
MatTableModule // adding MatTableDataSource here will get Error at component.ts: Unexpected value 'MatTableDataSource' imported by the module 'AppModule'. Please add a @NgModule annotation
]})
组件.ts
import {MatTableModule, MatTableDataSource} from '@angular/material/table';
@NgModule({
imports: [MatTableModule, MatTableDataSource]
})
component.html
<table mat-table [dataSource]="myArray">
<tr mat-row *matRowDef="let row"></tr>
</table>
package.json,也尝试了 v8.0.1 的材料:
"dependencies": {
"@angular/animations": "^8.0.0",
"@angular/cdk": "^8.0.1",
"@angular/common": "^8.0.0",
"@angular/compiler": "^8.0.0",
"@angular/core": "^8.0.0",
"@angular/flex-layout": "^8.0.0-beta.16",
"@angular/forms": "^8.0.0",
"@angular/http": "^7.2.8",
"@angular/material": "^8.1.2",
"@angular/platform-browser": "^8.0.0",
"@angular/platform-browser-dynamic": "^8.0.0",
"@angular/router": "^8.0.0",
"core-js": "^2.5.4",
"rxjs": "~6.3.3",
"rxjs-compat": "~6.3.3",
"tslib": "^1.9.0",
"zone.js": "~0.8.26"
},
尝试了所有这些: Angular7 - Can't bind to 'dataSource' since it isn't a known property of 'mat-table'
ERROR: Can't bind to 'dataSource' since it isn't a known property of 'mat-tree'
Material Design Table is not accepting dataSource object
mat-table can't bind dataSource
https://github.com/angular/components/issues/11906
【问题讨论】:
-
这里的问题到底是什么?
-
无法绑定到“dataSource”,因为它不是 Angular 运行时抱怨的“table”的已知属性。
-
你可以试试我的表格组件github.com/adriandavidbrand/ngx-ez 和stackblitz.com/edit/angular-npn1p1 的演示。比垫子更容易使用
-
出色的研究工作。
标签: angular