首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能绑定到“dataSource”,因为它不是表的已知属性

不能绑定到“dataSource”,因为它不是表的已知属性
EN

Stack Overflow用户
提问于 2018-05-08 07:26:35
回答 18查看 203.1K关注 0票数 141

我是新的角5发展。我正试图使用这里提供的示例"https://material.angular.io/components/table/examples“开发一个带有角材料的数据表。

我说Can't bind to 'dataSource' since it isn't a known property of 'table'.时出错了

请帮帮忙。

EN

回答 18

Stack Overflow用户

回答已采纳

发布于 2018-05-08 09:16:42

感谢@Jota.Toledo,我找到了创建表的解决方案。请查找以下工作代码:

component.html

代码语言:javascript
复制
<mat-table #table [dataSource]="dataSource" matSort>
  <ng-container matColumnDef="{{column.id}}" *ngFor="let column of columnNames">
    <mat-header-cell *matHeaderCellDef mat-sort-header> {{column.value}}</mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element[column.id]}}</mat-cell>
  </ng-container>

  <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

component.ts

代码语言:javascript
复制
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatTableDataSource, MatSort } from '@angular/material';
import { DataSource } from '@angular/cdk/table';

@Component({
  selector: 'app-m',
  templateUrl: './m.component.html',
  styleUrls: ['./m.component.css'],
})
export class MComponent implements OnInit {

  dataSource;
  displayedColumns = [];
  @ViewChild(MatSort) sort: MatSort;

  /**
   * Pre-defined columns list for user table
   */
  columnNames = [{
    id: 'position',
    value: 'No.',

  }, {
    id: 'name',
    value: 'Name',
  },
    {
      id: 'weight',
      value: 'Weight',
    },
    {
      id: 'symbol',
      value: 'Symbol',
    }];

  ngOnInit() {
    this.displayedColumns = this.columnNames.map(x => x.id);
    this.createTable();
  }

  createTable() {
    let tableArr: Element[] = [{ 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' },
    ];
    this.dataSource = new MatTableDataSource(tableArr);
    this.dataSource.sort = this.sort;
  }
}

export interface Element {
  position: number,
  name: string,
  weight: number,
  symbol: string
}

app.module.ts

代码语言:javascript
复制
imports: [
  MatSortModule,
  MatTableModule,
],
票数 52
EN

Stack Overflow用户

发布于 2018-08-15 10:25:17

记住在您的MatTableModule中添加app.module's imports,即

角9+

代码语言:javascript
复制
import { MatTableModule } from '@angular/material/table'  

@NgModule({
  imports: [
    // ...
    MatTableModule
    // ...
  ]
})

小于角度9

代码语言:javascript
复制
import { MatTableModule } from '@angular/material'  

@NgModule({
  imports: [
    // ...
    MatTableModule
    // ...
  ]
})
票数 198
EN

Stack Overflow用户

发布于 2018-08-05 10:25:34

  • 角芯v6.0.2,
  • 角材料,v6.0.2,
  • 角CLI v6.0.0 (全局v6.1.2)

我在运行ng test时遇到了这个问题,因此为了解决这个问题,我在xyz.component.spec.ts文件中添加了以下内容:

import { MatTableModule } from '@angular/material';

并将其添加到imports节中的TestBed.configureTestingModule({})中。

代码语言:javascript
复制
beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ ReactiveFormsModule, HttpClientModule, RouterTestingModule, MatTableModule ],
      declarations: [ BookComponent ],
      schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
    })
    .compileComponents();
}));
票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50228138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档