使用Material数据表时,dataSource.connect不是函数

时间:2018-03-04 22:51:35

标签: angular angular-material angular-material2 angular-datatables

我将使用dataSource作为数组的材质数据表。

import { Component, OnInit } from '@angular/core';
import { MatTableDataSource } from '@angular/material';
import { AjouttextService } from '../ajouttext.service';
import { DataSource } from "@angular/cdk/collections";
import { Observable } from "rxjs/Observable";
import { nomchamp } from '../model';

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

  constructor(private dataService: AjouttextService) { }
  data: nomchamp[] = [{ id: "33", text: "HAHA" }, { id: "55", text: "bzlblz" }];

  displayedColumns = ['text'];
  dataSource = this.data;



  applyFilter(filterValue: string) {
    filterValue = filterValue.trim(); // Remove whitespace
    filterValue = filterValue.toLowerCase(); // MatTableDataSource defaults to lowercase matches
  }
  ngOnInit() {
  }
}

当我运行我的应用程序时,我在html文件中遇到此错误,第2行:

  

错误类型错误:this.dataSource.connect不是函数

这里的html文件:

<div class="example-container mat-elevation-z8">
  <mat-table #table [dataSource]="dataSource">

    <!-- Position Column -->
    <ng-container matColumnDef="text">
      <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.text}} </mat-cell>
    </ng-container>



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

1 个答案:

答案 0 :(得分:4)

请参阅one of the examples from the docs

您需要以这种方式实例化MatTableDataSource

dataSource = new MatTableDataSource(yourDataArray);

在您的情况下,您还可以添加类型信息:

dataSource = new MatTableDataSource<nomchamp>(yourDataArray);