首页 > 解决方案 > 类型'Person []'不可分配给Angular 7 Datatable中的类型'ColumnSettings []

问题描述

问题

类型“Person[]”不可分配给类型“ColumnSettings[]”。

“Person”类型与“ColumnSettings”类型没有共同的属性。ts(2322)

预期的类型来自属性 'columns',它在类型 'Settings' 上声明

我已经关注了http://l-lin.github.io/angular-datatables/#/basic/server-side-angular-way

附加 json 数据表后出现问题。

人物类

export class Person {
id: number;
first_name: string;
last_name: string;
}

角码

publicDeals: Person[] = [];

ngOnInit(): void {
const that = this;
 


this.abc();



console.log(this.publicDeals);

this.dtOptions = {
  pagingType: 'full_numbers',
  pageLength: 2,
  serverSide: true,
  processing: true,
  ajax: (dataTablesParameters: any, callback) => {
    that.httpClient
      .post<DataTablesResponse>(
        this.api_url,
        dataTablesParameters, {}
      ).subscribe(resp => {
        that.persons = resp.data;
        //console.log(resp);
        callback({
          recordsTotal: resp.recordsTotal,
          recordsFiltered: resp.recordsFiltered,
          data: [],

        });
      });
     },
  
   columns: that.publicDeals,


};


 }


   abc() {

return this.service.apifunc()
.subscribe(persons => {
  this.testts = TABLE_FIELDS;
   
  
   TABLE_FIELDS.forEach(element => {
    this.publicDeals.push(element);
   });
    this.dtTrigger.next();
});

}

json数据

     TABLE_FIELD: [
        {
        data: "id"
        },
        {
        data: "first_name"
        },
        {
        data: "last_name"
        }

        ]

标签: angularangular7angular-datatablesangular-data

解决方案


this.dtOptions.columns是 参考https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e01d6e7abb2343c6b845d4945a368ed55cbf5bd2/types/datatables.net/index.d.ts#L1309type _ColumnSettings[]

但是您传递的Person[]打字稿编译器会引发错误。

您必须先修改数据,然后再分配给this.dtOptions.columns.


推荐阅读