0

I am trying to understand the DI in Angular 7. DI is used to instantiate the object for the class which is made as injectable.

Need to know how we are applying that for HttpClient.

I know we can inject it like below

constructor(private http: HttpClient) { }

But how we know that HttpClient is injectable?

Question looks silly. Since I am new to Angular, I trying to understand the logic.

Pac0
  • 21,465
  • 8
  • 65
  • 74
2727
  • 39
  • 1
  • 7

1 Answers1

2

Basically, because when HttpClientModule is imported, it tells to the Angular dependency injector that he can provide an HttpClient when needed.

This is done by

1) Having a class HttpClient decorated as @Injectable()

2) Having a module HttpClientModule which imports and, more importantly, provides the HttpClient.

3) Of course all is done by Angular dependency injector, which is a fundamental brick of the framework. It is doing the work of looking at the various modules and their providers config parameter. (and some other related, for Angular 6+)


You should basically do the same (1 and 2) if you want to create your own injectable services.


Documentation and relevant Angular source code snippets below :

https://angular.io/api/common/http/HttpClientModule

HttpClientModule

Configures the dependency injector for HttpClient with supporting services for XSRF. Automatically imported by HttpClientModule.


source code for HttpClient:

(around line 56)

/**
 * Perform HTTP requests.
 *
 * ....
 */
@Injectable()   // <- this marks the class as Injectable (which basically makes it a Service in Angular framework)
export class HttpClient {
// ...

Source code for HttpClientModule

(around line ~143)

@NgModule({
  /**
   (...)
   * Configures the [dependency injector](guide/glossary#injector) where it is imported
   * with supporting services for HTTP communications.
   */
  providers: [
    HttpClient,    // <- here this is how the injector system is aware of HttpClient being injectable, if you import this module
  (...)
  ]
})
export class HttpClientModule {
}

(my own comments in above snippets noted with // <-) .


Also worth reading :

https://angular.io/guide/glossary#injector

https://angular.io/guide/dependency-injection


One nice interesting blog about the details, and the modern (Angular 6+) way of doing injection (with tree-shaking capabilities) :

https://medium.com/@tomastrajan/total-guide-to-angular-6-dependency-injection-providedin-vs-providers-85b7a347b59f

Another that goes more in the mechanics of the dependency injection system (of Angular 2) : https://blog.thoughtram.io/angular/2015/05/18/dependency-injection-in-angular-2.html#dependency-injection-in-angular

And a last one that goes a bit more deep technical : https://blog.angularindepth.com/angular-dependency-injection-and-tree-shakeable-tokens-4588a8f70d5d

Pac0
  • 21,465
  • 8
  • 65
  • 74
  • Thank you . In VS code, when i hit alt+F12 on httpclient,it opened client.d.ts file, in that i was not able to find @injectable for HttpClient class. So got confused. – 2727 Jan 31 '19 at 03:44
  • in client.d.ts: This is what i found : /** * Perform HTTP requests. * * `HttpClient` is available as an injectable class, with methods to perform HTTP requests. * Each request method has multiple signatures, and the return type varies according to which * signature is called (mainly the values of `observe` and `responseType`). * * @publicApi */ export declare class HttpClient { **** } – 2727 Jan 31 '19 at 03:50
  • 1
    @mi_27 Oh, interesting, I completely understand why you are confused. I may oversimplify things in the following comment, but : `*.d.ts` files are *definition files* for the TypeScript compiler, actually. They are like "hints" for TypeScript to be quickly aware of the various types that your project is using. Don't consider them as the source code. All the dependency stuff is done by the "Angular compiler" (part of the framework), which is a bunch of TS files with lots. And it uses the actual .ts code (*not* the *.d.ts* files). They should be mentioned in "tsconfig.json" file. – Pac0 Jan 31 '19 at 05:57
  • I just read again my comment and sentences don't really make sense, but I hope you get the idea. – Pac0 Jan 31 '19 at 06:04
  • Thank u for helping me out . My doubt is clarified now. – 2727 Jan 31 '19 at 11:58