2016-07-13 14 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

あなたの問題は、ここでは "./htttp.service" から 'インポート{}のHTTPServiceである;' 'htttp'それだけで2 – AngJobs

+0

感謝をすべきで、あなたが3 'T'を持っています!どのような愚かな間違い! :) –

+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] 
}) 
関連する問題