angular7 ag-grid this.http - неопределенная ошибка - PullRequest
0 голосов
/ 23 апреля 2019

Я пытался использовать ag-grid в angular7, мой код выглядит следующим образом:


    import { Component, OnInit } from '@angular/core';
    import { HttpClient, HttpHeaders } from '@angular/common/http';

    import { AgGridModule} from 'ag-grid-angular';

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


      private top100url = 'http://resturl';

      private gridOptions;
      private row_per_page = 20;

      private endpoint;
      private rowData;
      private restDatasource;

      private columnDefs = [
        .
        .
        .
      ];

      constructor(private http: HttpClient) { }

      ngOnInit() {
          this.gridOptions = {
              columnDefs: this.columnDefs,
              rowModelType: 'infinite',
              //datasource: this.restDatasource,
              enableServerSideFilter: false,
              enableServerSideSorting: false,
              pagination: true, 
              paginationPageSize: this.row_per_page
         };
      }

      gridReady($event) {
          console.log("onGridReady "+$event.api.paginationGetPageSize());
          this.restDatasource = {
              rowCount: null,
              getRows: function(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);
                  });
              }   
          };  
          $event.api.setDatasource(this.restDatasource);
      };

    }

При инициализации страницы я получил следующую ошибку в консоли javascript.

ERROR TypeError:"this.http не определено"

Почему this.http не определено?Я внедряю его через конструктор.

У меня есть опыт работы с Angular UI Grid, есть ли подобное решение для angular 7?

1 Ответ

1 голос
/ 23 апреля 2019

Используйте функцию стрелки для определения 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);
     });
 }   
...