2016-07-06 86 views
1

我有這個代碼注入一個服務,它使一個http.get請求成爲一個組件。Angular 2 Typescript - 無法解析TooltipService的所有參數

該組件呈現正常,但是當我將服務注入組件時出現錯誤。

並得到錯誤;

EXCEPTION:無法解析TooltipService的所有參數:(?)。

我已經包含了自舉,因爲我相信它是一個DI問題,

import { Component, Input } from '@angular/core'; 
import { TooltipService } from '../../services/tooltip/tooltip.service'; 

@Component({ 
    .., 
    .., 
    providers: [TooltipService] 
}) 

export class TooltipComponent implements OnInit { 
    @Input() explaination: boolean; 
    @Input() nodeId: number; 
    @Input() questionId: number; 

    constructor(public tooltipService: TooltipComponent) {} 

    ngOnInit() { 
     if(this.explaination) { 
      this.tooltipService.getExplaination(this.nodeId, this.questionId) 
       .then(response => console.log(response)); 
     } 
    } 
} 

-

import { Http } from '@angular/http'; 
import { Injectable } from '@angular/core'; 
import 'rxjs/add/operator/toPromise'; 

@Injectable() 

export class TooltipService { 
    constructor(public http: HTTP) {} 

    getExplaination(private nodeId: number, private questionId: number) { 

     let url = `someUrl/` 
        + questionId + `/` + nodeId; 

     return this.http.get(url) 
      .toPromise() 
      .then(response => response.json()) 
      .catch(this.handleError); 
    } 

} 

-

import { bootstrap } from '@angular/platform-browser-dynamic'; 
import { disableDeprecatedForms, provideForms } from '@angular/forms'; 
import { HTTP_PROVIDERS } from '@angular/http'; 

import {AppComponent} from '../app/components/app/app.component'; 

bootstrap(AppComponent, [ 
     HTTP_PROVIDERS, 
     disableDeprecatedForms(), 
     provideForms() 
    ]) 
    .catch((err: any) => console.error(err)); 

回答

2

你在你的代碼2個錯別字。

Http,不HTTP

export class TooltipService { 
    constructor(public http: Http) {} 

你可能要注入的服務,而不是組件:

export class TooltipComponent implements OnInit { 
    @Input() explaination: boolean; 
    @Input() nodeId: number; 
    @Input() questionId: number; 

    constructor(public tooltipService: TooltipService) {} 
+0

洛爾多麼愚蠢的錯誤 –

+1

偏偏是最好的! :) – rinukkusu

+0

你介意接受解決問題的答案嗎? – rinukkusu

相關問題