Angular2:无法读取未定义的 属性 'post'

Angular2: Cannot read property 'post' of undefined

下面是我的post数据代码。我所有的获取请求都工作正常。当我执行 post 请求时,我得到 "Cannot read property 'post' of undefined"

@Injectable()
export class UserService {
    constructor(public _http: Http) { }

        postData(id: any) {
        let data = { "jql": id };
                    let body = JSON.stringify(data);
                    console.log(body);
                    return this._http.post('/api/Desk/Tickets/Search', body, { headers: this.getHeaders() })
                        .map((res: Response) => res.json());
    }
}

调用函数

  UserService.prototype.postData(id).subscribe(data=> {
          console.log('In response');
        });

虽然这有效:

UserService.prototype.postData(id).subscribe(data=> {
          console.log('In response');
        });

您破坏了服务中的 "this" 引用并绕过了 Angular 依赖项注入。您需要在调用它的地方注入您的服务并使用:

_localServiceInstance.postData(...) instead of UserService.prototype...

更新

@AJT_82 是对的,您的函数语法错误,这弄乱了 this 引用。应该是:

legendItemClick: (e) => {
    console.log(this);
    return false;
}

这也是疯狂的不可读。用空对象定义事件系列:

series: {
    animation: false,
    point: {
        events: {}
    }
}

然后在选项声明之后或文件中的其他地方,您可以简单地分配事件:

this.options.plotOptions.series.point.events.legendItemClick = function(e) {
    console.log(this);
    return false;
};

这是一个工作版本: https://plnkr.co/edit/ZJQeGLbz3OkR3mkpqc7j?p=info