angular7 ag-grid this.http 未定义错误
angular7 ag-grid this.http is undefined error
我尝试在 angular7 中使用 ag-grid,我的代码如下所示:
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 控制台中收到以下错误。
错误类型错误:"this.http is undefined"
为什么 this.http 未定义?我通过构造函数注入它。
我有 Angular UI 网格的经验,angular 7 有类似的解决方案吗?
使用箭头函数定义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);
});
}
我尝试在 angular7 中使用 ag-grid,我的代码如下所示:
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 控制台中收到以下错误。
错误类型错误:"this.http is undefined"
为什么 this.http 未定义?我通过构造函数注入它。
我有 Angular UI 网格的经验,angular 7 有类似的解决方案吗?
使用箭头函数定义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);
});
}