Angular2 - 无法导入http模块

时间:2017-11-18 08:34:36

标签: angular typescript node-modules angular-httpclient

Answer没有帮我导入http模块而没有错误。

$ ng version
Angular CLI: 1.5.0
Node: 6.11.5
OS: linux x64
Angular: 5.0.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cli: 1.5.0
@angular-devkit/build-optimizer: 0.0.32
@angular-devkit/core: 0.0.20
@angular-devkit/schematics: 0.0.35
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.8.0
@schematics/angular: 0.1.0
typescript: 2.4.2
webpack: 3.8.1
$

代码下方

/* ../src/app/app.module.ts */
import {HttpClientModule} from '@angular/common/http';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    DirectoryComponent,
    FilterPipe,
    LoggingService
  ],
  imports: [
    FormsModule,
    BrowserModule,
    HttpClientModule,
    routing
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

和服务代码,

/*  ../src/app/data.service.ts */
import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';

@Injectable()
export class DataService {

  constructor(private http: HttpClient) { }

}

给出错误:

ERROR in src/app/data.service.ts(2,9): error TS2305: Module '"/home/../My_Websites/Youtube_sites/angular_2_playlist/ninja-directory/node_modules/@angular/http/http"' has no exported member 'HttpClient'.

为什么不能在服务代码的构造函数中注入HttpClient服务?

1 个答案:

答案 0 :(得分:1)

您需要在app.component.ts中调用您的服务

import {DataService} from './data.service.ts';
  

并提供 app.component.ts 文件

providers:[DataService]

See TutorialsPoint Example for More

<强>感谢