【问题标题】:AG-GRID - Unable to fetch data from RESTAG-GRID - 无法从 REST 获取数据
【发布时间】:2018-07-17 02:14:27
【问题描述】:

我一直在用我认为很简单的代码来拉头发。我承认我对打字稿和学习很陌生,但对 javascript 更熟悉。

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

我已经在互联网上搜寻了几个星期的解决方案,但找不到与我的情况相匹配的示例。这里有人知道如何处理下面的错误吗?

控制台错误:

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)) }

}

【问题讨论】:

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


    【解决方案1】:

    首先,数据应该作为参数而不是赋值传递:

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

    【讨论】:

    • 好的,谢谢,试一试,但仍然看到相同的错误:错误 TS2345:“事件模型”类型的参数不可分配给“任何 []”类型的参数。 “Incident_Model”类型中缺少属性“includes”。
    • 在重新加载我的 Angular CLI (ng serve) 之后,错误神奇地消失了。我认为这是我的一个愚蠢的错误,我认为它比那个更复杂,哈哈。再次感谢亚历山大!
    猜你喜欢
    • 2020-03-06
    • 2021-02-10
    • 1970-01-01
    • 2022-07-24
    • 2020-05-31
    • 2019-08-26
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    相关资源
    最近更新 更多