AG-GRID - 无法从REST获取数据

时间:2018-02-06 22:09:24

标签: javascript angular typescript ag-grid ag-grid-ng2

我一直在用我想要的东西拉头发将是一个简单的代码。我承认我对打字稿和学习很新,但我更熟悉javascript。

我基本上使用AG-GRID作为我的缅因组件创建SPA(单页应用程序)。我有一个服务从REST连接(当前使用JSON服务器来模仿它)中提取数据,并将跨多个组件共享该数据。 AG-GRID似乎是目前唯一拒绝正常工作的组件。我收到以下错误。

我已经在网上搜索了几个星期的解决方案,但是找不到符合我情况的exmaple。这里的任何人都知道如何处理下面的错误吗?

控制台错误:

error TS2345: Argument of type '(data: Incident_Model) => Incident_Model' is not assignable to parameter of type '(value: Incident_Model[]) => void'.
  Types of parameters 'data' and 'value' are incompatible.
    Type 'Incident_Model[]' is not assignable to type 'Incident_Model'.
      Property 'id' is missing in type 'Incident_Model[]'.

角度界面:

export class Incident_Model {
    id: number;
    Incident: string;
    status: string;
}

角度服务:

import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';

import { HttpClient } from "@angular/common/http";
import { Observable } from 'rxjs/Observable';
import { Incident_Model } from './incident_model';

const BackEnd_URL = environment.BackEnd_URL;

@Injectable()
export class BackEndService {

  constructor(private http: HttpClient ) { }

  getAllIncidents(): Observable<Incident_Model[]> {

    return this.http.get<Incident_Model[]>(BackEnd_URL + '/Incidents');

  }

}

角度组件:

ngOnInit() {this.backendservice.getAllIncidents().subscribe( data => this.gridOptions.api.setRowData = data )}

更新了以下评论中的代码 - 11h37 - 2018-02-07: 以下是完整的组件代码,以防有人发现我遗漏的东西:

import { Component, OnInit } from '@angular/core';
import { GridOptions } from "ag-grid";
import { BackEndService } from '../../Shared/back-end.service';
import { Incident_Model } from '../../Shared/incident_model';

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

  private gridOptions: GridOptions;

  constructor(private backendservice: BackEndService) { 

    var gridSize = 8;
    var rowHeight = gridSize * 6;
    var headerHeight = gridSize * 7;

    this.gridOptions = <GridOptions>{
      enableFilter: true,
      enableColResize: true,
      enableSorting: true,
      pagination: true,
      paginationPageSize:25,
      animateRows: true,
      headerHeight: headerHeight,
      rowHeight:rowHeight         
    };

    this.gridOptions.columnDefs = [
      {
        headerName: "Headname here",
        children:[
        {
              headerName: "id",
              field: "id",
              width: 165,
              filterParams: { applyButton: true, clearButton:true }
          },
          {
              headerName: "Incident",
              field: "Incident",
              width: 450,
              filterParams: { applyButton: true, clearButton:true }
          },
          {
            headerName: "status",
            field: "status",
            width: 110,
            filterParams: { applyButton: true, clearButton:true }
          }
    ];

  }

  ngOnInit() {this.backendservice.getAllIncidents().subscribe(data => this.gridOptions.api.setRowData(data)) }

}

1 个答案:

答案 0 :(得分:1)

首先,数据应作为参数而不是分配传递:

this.backendservice.getAllIncidents().subscribe(data => this.gridOptions.api.setRowData(data));