2017-11-18 150 views
-1

Answer沒有幫助我導入http模塊沒有錯誤。Angular2 - 無法導入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

什麼是您正在使用的 –

+1

角版本錯誤'node_modules/@ angular/ht tp/http「'沒有導出的成員」狀態,表示你從其他地方導入它 – yurzui

+0

你在使用Systemjs嗎? – yurzui

回答

1

你需要打電話給你的服務app.component.ts

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

and also give in your app.component.ts file

providers:[DataService] 

See TutorialsPoint Example for More

感謝

+0

'providers'和'bootstrap'是什麼意思?他們的目的是什麼? – overexchange

+0

Downvote from me – overexchange