Angular 运行时未定义注入的HTTP客户端

Angular 运行时未定义注入的HTTP客户端,angular,dependency-injection,angular-httpclient,Angular,Dependency Injection,Angular Httpclient,我试图在服务中使用Angular的HttpClient,但在运行时遇到以下错误: ERROR TypeError: "this.http is undefined" 我正在我的app.module.ts中导入HttpClientModule: …并在my app.service.ts中导入HttpClient: 是否因为它位于新对象中而引用了不同的范围?我是否需要以某种方式传递http,或者依赖项注入是否涵盖了这一点?请尝试使用arrow函数确保您的此上下文引用了当前类实例: 之前: byKe

我试图在服务中使用Angular的HttpClient,但在运行时遇到以下错误:

ERROR TypeError: "this.http is undefined"
我正在我的app.module.ts中导入HttpClientModule:

…并在my app.service.ts中导入HttpClient:


是否因为它位于新对象中而引用了不同的范围?我是否需要以某种方式传递http,或者依赖项注入是否涵盖了这一点?

请尝试使用arrow函数确保您的此上下文引用了当前类实例:

之前:

byKey: function (key) {
之后:

byKey: key => {
byKey: function (key) {
byKey: key => {