Angular 2 注入服务未定义
Angular 2 Undefined on Injected Service
我有一个 Angular 2 组件,它实现了一个接口 IDatasource 作为 AG-GRID 的一部分
我无法使注入的 httpClient 服务在 dataSource getRows 函数中可用。
我假设这与我不理解的注入有关;但我想不通为什么。
constructor(private httpClient: HttpClient) {
//This one works perfectly fine and httpClient is defined.
console.log(this.httpClient);
this.gridOptions = <GridOptions>{
//Removed other config for brevity.
datasource: {
getRows: function (params) {
let data: any[] = [];
//this one - the httpClient is undefined. No idea why?
console.log(this.httpClient);
// this.httpClient.post('incidents/GetIncidentList', params).subscribe(response => data = response.json());
let lastRow = -1;
params.successCallback(data, lastRow);
}
}
};
}
还尝试将其定义为单独的 属性 并遇到完全相同的问题。
private dataSource: IDatasource = {
getRows: function (params) {
let data: any[] = [];
console.log(this.httpClient);
// this.httpClient.post('incidents/GetIncidentList', params).subscribe(response => data = response.json());
let lastRow = -1;
params.successCallback(data, lastRow);
}
}
如果你想使用'thisinside the callback use
() =>instead of
function()`
getRows: function (params) {
应该是
getRows: (params) => {
我有一个 Angular 2 组件,它实现了一个接口 IDatasource 作为 AG-GRID 的一部分
我无法使注入的 httpClient 服务在 dataSource getRows 函数中可用。
我假设这与我不理解的注入有关;但我想不通为什么。
constructor(private httpClient: HttpClient) {
//This one works perfectly fine and httpClient is defined.
console.log(this.httpClient);
this.gridOptions = <GridOptions>{
//Removed other config for brevity.
datasource: {
getRows: function (params) {
let data: any[] = [];
//this one - the httpClient is undefined. No idea why?
console.log(this.httpClient);
// this.httpClient.post('incidents/GetIncidentList', params).subscribe(response => data = response.json());
let lastRow = -1;
params.successCallback(data, lastRow);
}
}
};
}
还尝试将其定义为单独的 属性 并遇到完全相同的问题。
private dataSource: IDatasource = {
getRows: function (params) {
let data: any[] = [];
console.log(this.httpClient);
// this.httpClient.post('incidents/GetIncidentList', params).subscribe(response => data = response.json());
let lastRow = -1;
params.successCallback(data, lastRow);
}
}
如果你想使用'thisinside the callback use
() =>instead of
function()`
getRows: function (params) {
应该是
getRows: (params) => {