2016-07-13 41 views
-1

當我運行下面的項目中,我得到的錯誤:無提供HttpService的

No provider for HttpService! (AppComponent -> HttpService)

有人能幫助我嗎?

我AppComponent:

import {Component} from 'angular2/core'; 
import {HttpService} from "./htttp.service"; 

@Component({ 
    selector: 'my-app', 
    template: ` 
    <div> 
    <div class="input"> 
    <label for="title">Title</label> 
    <input type="text" id="title" #title> 
    </div> 
    <div class="body"> 
    <label for="body">Body</label> 
    <input type="text" id="body" #body> 
    </div> 
    <div class="user-id"> 
    <label for="user-id">User ID</label> 
    <input type="text" id="user-id" #userid> 
    </div> 
    <button (click)="onPost(title.value, body.value, userid.value)">Post Data</button> 
    <button (click)="onGetPosts()">Get All Posts</button> 
    <p>Response: {{response | json}}</p> 
    </div>, 

    providers: [HttpService] 
     `, 
}) 
export class AppComponent { 
    response: string; 

    constructor(private _httpService: HttpService){} 
    onGetPosts(){ 
    this._httpService.getPosts().subscribe(
     response => this.response=response, 
     error => console.log(error) 
    ) 
    } 

} 

HttpService的:

import {Injectable} from 'angular2/core'; 
import {Http} from "angular2/http"; 
import {Observable} from "rxjs/Observable"; 
import 'rxjs/Rx'; 

@Injectable() 

export class HttpService{ 
    constructor(private _http:Http){} 

    getPosts():Observable<any>{ 
    return this._http.get('http://jsonplaceholder.typicode.com/posts').map(res => res.json()); 
    } 

} 

和boot.ts:

import {bootstrap} from 'angular2/platform/browser'; 
import {AppComponent} from "./app.component"; 
import {HTTP_PROVIDERS} from "angular2/http"; 

bootstrap(AppComponent, [HTTP_PROVIDERS]); 

問題出在哪裏?

+0

您的問題是在這裏'進口{} HttpService的從 「./htttp.service」;'你有'htttp' 3't'只宜2 – AngJobs

+0

謝謝!多麼愚蠢的錯誤! :) –

+0

不用擔心。現在點擊'UP'按鈕!謝謝! – AngJobs

回答

1

應該

import {HttpService} from "./http.service"; 
0

你忘了聲明你提供之前關閉模板文字「回報價」。

@Component({ 
    selector: 'my-app', 
    template: ` 
    <div> 
    <div class="input"> 
    <label for="title">Title</label> 
    <input type="text" id="title" #title> 
    </div> 
    <div class="body"> 
    <label for="body">Body</label> 
    <input type="text" id="body" #body> 
    </div> 
    <div class="user-id"> 
    <label for="user-id">User ID</label> 
    <input type="text" id="user-id" #userid> 
    </div> 
    <button (click)="onPost(title.value, body.value, userid.value)">Post Data</button> 
    <button (click)="onGetPosts()">Get All Posts</button> 
    <p>Response: {{response | json}}</p> 
    </div>`, 
    providers: [HttpService] 
})