angular7 ag-grid this.http 是未定义的错误

Vin*_*hen 5 ag-grid angular angular7

我尝试在 angular7 中使用 ag-grid,我的代码如下所示:

    从 '@angular/core' 导入 { Component, OnInit };
    从 '@angular/common/http' 导入 { HttpClient, HttpHeaders };

    从“ag-grid-angular”导入{AgGridModule};

    @成分({
      选择器:'app-top100sp',
      templateUrl: './top100sp.component.html',
      styleUrls: ['./top100sp.component.css']
    })
    导出类 Top100spComponent 实现 OnInit {


      私有top100url = 'http://resturl';

      私有网格选项;
      私有每页行= 20;

      专用端点;
      私有行数据;
      私有休息数据源;

      私有columnDefs = [
        。
        。
        。
      ];

      构造函数(私有http:HttpClient){}

      ngOnInit() {
          this.gridOptions = {
              columnDefs: this.columnDefs,
              rowModelType: '无限',
              //数据源:this.restDatasource,
              启用服务器端过滤器:假,
              启用服务器端排序:假,
              分页:真实,
              分页页面大小:this.row_per_page
         };
      }

      网格就绪($事件){
          console.log("onGridReady "+$event.api.paginationGetPageSize());
          this.restDatasource = {
              行数:空,
              getRows:函数(参数){
                  console.log(params.startRow + " 到 " + params.endRow);
                  this.endpoint = this.top100url + "/"+ params.startRow +"/" + params.endRow;
                  this.http.get(this.endpoint).subscribe((结果) => {
                      //console.log(结果);
                      //this.rowData = 结果;
                        params.successCallback(结果, 20);
                  });
              }   
          };  
          $event.api.setDatasource(this.restDatasource);
      };

    }

当页面初始化时,我在 javascript 控制台中收到以下错误。

错误类型错误:“this.http 未定义”

为什么 this.http 未定义?我通过构造函数注入它。

我有 Angular UI Grid 的经验,Angular 7 有类似的解决方案吗?

Par*_*osh 4

使用箭头函数来定义getRows方法。

getRows = (params) => {
     console.log(params.startRow + " to " + params.endRow);
     this.endpoint = this.top100url + "/"+ params.startRow +"/" + params.endRow;

     this.http.get(this.endpoint).subscribe((results) => {
        //console.log(results);
        //this.rowData = results;
        params.successCallback(results, 20);
     });
 }   
Run Code Online (Sandbox Code Playgroud)