角度MatTableDataSource错误 [英] Angular MatTableDataSource Error

查看:20
本文介绍了角度MatTableDataSource错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到如下错误:

[ts]类型‘Company’的参数不能赋值给类型的参数 ‘COMPANY[]’。类型‘Company’中缺少属性‘Includes’。

当我想要将数组对象插入MatTableDataSource时。这是我的打字稿文件:

import { Component, OnInit, ViewChild } from '@angular/core';
import { MatTableModule } from '@angular/material/table';
import { MatTableDataSource, MatPaginator, MatSort } from '@angular/material';
import { CompanyService } from '../../../services/company.service';
import { AuthService } from '../../../services/auth.service';
import { DataSource } from '@angular/cdk/collections';
import { Company } from '../../../models/company.model';
import { Observable } from "rxjs";
import { Router } from '@angular/router';
import { filter } from 'rxjs/operators';
@Component({
  selector: 'app-index-company',
  templateUrl: './index-company.component.html',
  styleUrls: ['./index-company.component.css']
})

export class IndexCompanyComponent implements OnInit {
  company   : Object;
  companies : Object;
  displayedColumns = ['perusahaan', 'kategori', 'mahasiswa', 'option'];
  dataSource: MatTableDataSource<Company>;
  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

  applyFilter(filterValue: string) {
    filterValue = filterValue.trim(); 
    filterValue = filterValue.toLowerCase();
    this.dataSource.filter = filterValue; 
  }
  constructor(
    private companyService: CompanyService,
    private authService: AuthService,
    private router: Router
  )
  {
      this.companyService.getCompanies().subscribe(companies => {
      this.companies = companies;
      this.dataSource = new MatTableDataSource(companies);
    },
    err => {
      console.log(err);
      return false;
    });
  }

  ngOnInit() {
  }
}

这是我的公司型号:

export class Company{
    perusahaan       : String;
    alamat           : String;
    deskripsi        : String;
    telepon          : String;
    email_perusahaan : String;
    website          : String;
    students         = [];
    kategori         : String;
    author           : String;
    update_by        : String;
    status           : String;
}

已编辑。已添加CompanyService:

  getCompanies(): Observable<Company>{
    let headers = new Headers();
    headers.append('Authorization', this.authToken);
    headers.append('Content-Type', 'application/json');
    return this.http.get(this.baseUri+'/companies', {headers: headers})
    .map(res => res.json());
  }

推荐答案

打字稿上个月变得更加严格,因此现在的解决方案是:

dataSource: MatTableDataSource<any[]> = new MatTableDataSource<any[]>([]);

或者您可以禁用--strictPropertyInitialization

这篇关于角度MatTableDataSource错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆