【问题标题】:angular7 ag-grid this.http is undefined errorangular7 ag-grid this.http 是未定义的错误
【发布时间】:2019-09-12 11:39:09
【问题描述】:

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

从'@angular/core'导入{组件,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'; 私有网格选项; 私有 row_per_page = 20; 私有端点; 私有行数据; 私人休息数据源; 私有 columnDefs = [ . . . ]; 构造函数(私有http:HttpClient){} ngOnInit() { this.gridOptions = { columnDefs:this.columnDefs, rowModelType: '无限', //数据源:this.restDatasource, enableServerSideFilter: false, enableServerSideSorting:假, 分页:真实, paginationPageSize:this.row_per_page }; } 网格就绪($事件){ console.log("onGridReady "+$event.api.paginationGetPageSize()); this.restDatasource = { 行数:空, getRows:函数(参数){ console.log(params.startRow + " to " + params.endRow); this.endpoint = this.top100url + "/"+ params.startRow +"/" + params.endRow; this.http.get(this.endpoint).subscribe((results) => { //console.log(结果); //this.rowData = 结果; params.successCallback(结果, 20); }); } }; $event.api.setDatasource(this.restDatasource); }; }

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

ERROR TypeError: "this.http is undefined"

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

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

【问题讨论】:

    标签: angular angular7 ag-grid


    【解决方案1】:

    使用箭头函数定义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);
         });
     }   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-15
      • 2021-05-20
      • 2018-01-16
      • 2021-01-21
      • 1970-01-01
      • 2017-03-16
      • 2020-08-31
      • 2020-03-31
      相关资源
      最近更新 更多