【问题标题】:Still "Can't bind to 'dataSource' since it isn't a known property of 'table'."仍然“无法绑定到 'dataSource',因为它不是 'table' 的已知属性。”
【发布时间】:2019-08-12 00:34:28
【问题描述】:

我花了两天时间寻找所有可能的解决方案(见下文),并将 Angular 和 Material 从 7 升级到 8,仍然是同样的抱怨。

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

  1. import@NgModule 在 app.module.ts 和 component.ts 中 (也尝试了部分,所有可能的组合`)
  2. <table 换成<mat-table
  3. [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

https://www.devexpress.com/Support/Center/Question/Details/T730641/the-can-t-bind-to-datasource-since-it-isn-t-a-known-property-of-dx-data-grid-error-occurs

https://www.oipapio.com/question-941474

https://code-examples.net/en/q/2fe6baa

【问题讨论】:

标签: angular


【解决方案1】:

花了一段时间才弄明白,“组件”在一个“模块”中,因为这个结构 app\module\component,所以不管app.module.ts 中的一切都是正确的,它们都不正确容器。将必要的import@NgModule({ imports: [] }) 添加到module.ts 后,就可以了!

【讨论】:

    猜你喜欢
    • 2019-04-10
    • 1970-01-01
    • 2018-12-04
    • 2022-11-27
    • 1970-01-01
    • 2017-06-21
    • 2017-02-03
    • 1970-01-01
    • 2019-10-10
    相关资源
    最近更新 更多