How to use/import http module?

前端 未结 13 2283
鱼传尺愫
鱼传尺愫 2020-11-27 16:40

I\'ve been playing with Angular 2 Quickstart.

How can I use/import http module in Angular 2?

I\'ve looked at Angular 2 Todo\'s.js, but it doesn\'t use the ht

13条回答
  •  爱一瞬间的悲伤
    2020-11-27 17:20

    For Angular 4.3+, 5.+

    // app.module.ts:
    
    import {NgModule} from '@angular/core';
    import {BrowserModule} from '@angular/platform-browser';
    
    // Import HttpClientModule from @angular/common/http
    import {HttpClientModule} from '@angular/common/http';
    
    @NgModule({
      imports: [
        BrowserModule,
        // Include it under 'imports' in your application module
        // after BrowserModule.
        HttpClientModule,
      ],
    })
    export class MyAppModule {}
    

    And inside your service class

    import { HttpClient } from '@angular/common/http';
    

    Other packages you might also need

    import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse, HttpErrorResponse } from '@angular/common/http';
    

    In package.json

    "@angular/http": "^5.1.2",
    

    Reference is here

提交回复
热议问题