如何实例化Angular HttpClient? [英] How to instantiate an Angular HttpClient?

查看:118
本文介绍了如何实例化Angular HttpClient?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Angular 5中实例化HttpClient?我需要在构造函数中实例化它,而不是在constructor(private _Http: HttpClient)这一行中.

How do you instantiate an HttpClient in Angular 5? I need to instantiate it inside the constructor, but not in this line constructor(private _Http: HttpClient).

也许是这样的假设示例:

Maybe something like this hypothetical example:

import { HttpClient, HttpHandler } from '@angular/common/http';
private _Http: HttpClient;
private _httpHandler: HttpHandler;
@Injectable()
export class DataService {
    constructor() {
        this._Http = new HttpClient(_httpHandler);
    }
}

谢谢

推荐答案

您应该真正使用依赖项注入,因为它非常干净和容易:

You should really use dependency injection, since it's very clean and easy:

constructor(private http: HttpClient) {}

HttpClient 就是这样打算使用所有Angular服务.在此处阅读有关DI的更多信息: https://angular.io/guide/dependency-injection .

但是,由于它只是一门课程,因此从技术上讲,您无需DI就可以做到这一点.

However, you can technically do it without DI, since it is just a class.

您目前对this._Http = new HttpClient(_httpHandler);所做的操作的问题是HttpClient需要一个HttpHandler的实例,但是现在它只是获取一个没有值的变量_httpHandler键入为HttpHandler.您需要这样做:

The issue with what you're currently doing with this._Http = new HttpClient(_httpHandler); is that the HttpClient requires an instance of an HttpHandler, but right now it's just getting a variable with no value called _httpHandler typed as an HttpHandler. You need to do this:

let _httpHandler = new HttpHandler();

@Injectable()
export class DataService {
    constructor() {
        this._Http = new HttpClient(_httpHandler);
    }
}

那应该可以使它工作",但是我还是建议您再看一次依赖注入.

That should get it to "work", but again I'd recommend taking another look at dependency injection.

更新:

就像乔塔一样.托莱多在注释中指出,您实际上无法实例化HttpHandler,因为它是抽象类.在此处查看源代码:

So as Jota. Toledo pointed out in the comments, you actually cannot instantiate an HttpHandler as it is an abstract class. See the source code here: https://github.com/angular/angular/blob/5.2.1/packages/common/http/src/backend.ts#L12-L27.

所以这变得更加复杂.

对于@Component,有一种直接与喷油器配合使用的方式,直接 Angular团队明确建议.

For @Component's, there is a way to work with injectors directly that the Angular team explicitly advises against.

在@Component元数据中,提供要直接使用的服务(在providers数组中),如下所示:

In your @Component metadata, provide the service you want to use directly (in the providers array), like so:

@Component({
 providers: [HttpClient]
}
export class MyComponent {}

然后您可以使用咳嗽注入 Injector >依赖注入.像这样访问您的提供程序中的提供程序:

And then you can inject the Injector using cough Dependency Injection. And access your providers in the constructor like so:

constructor(private injector: Injector) {
  this._http = injector.get(HttpClient);
}

尽管如此,考虑到您在问题中显示了@Injectable,而它本身没有元数据,因此我认为这不适用于您的用例.其次,您已经在使用依赖项注入来获取Injector,因此您最好将DI用于HttpClient.

Although, I don't think this will work for your use case considering you show an @Injectable in your question, which doesn't have metadata per se. Second, you're already using dependency injection to get ahold of the Injector so you might as well just use DI for HttpClient.

似乎您仍然可以使用已弃用的 ReflectiveInjector @Injectable类.

It seems you can also still use the deprecated ReflectiveInjector to do this with an @Injectable class.

总而言之,这是一个疯狂的追求,您应该真正使用依赖注入.它是Angular的基本概念,也是使框架如此有用的一件事.如果由于某种原因无法使用它,则可能需要查看Angular之外的其他选项.

In conclusion, this is a wild goose chase and you should really use dependency injection. It is a fundamental concept in Angular and one of the things that makes the framework so useful. If for some reason you cannot use it, you may want to look at other options besides Angular.

这篇关于如何实例化Angular HttpClient?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆