[英]Angular 2 Undefined on Injected Service
我有一個Angular 2組件,該組件實現了作為AG-GRID一部分的接口IDatasource
我無法從dataSource getRows函數中獲得注入的httpClient服務。
我假設這與我不了解的注射有關。 但是我不能為為什么而動。
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);
}
}
如果要inside the callback use
使用´this inside the callback use
()=> instead of
function()
getRows: function (params) {
應該
getRows: (params) => {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.