角度 2 在注入的服务上未定义



>我有一个 Angular 2 组件,它实现了一个接口 IDatasource 作为 AG-GRID 的一部分

我无法从数据源获取行函数中提供注入的httpClient Service。

假设这与注射有关,我只是不明白; 但我无法理解为什么。

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);
    }
}

如果你想使用'this inside the callback use () => instead of function()'

getRows: function (params) {

应该是

getRows: (params) => {

相关内容

  • 没有找到相关文章

最新更新