2017-03-12 8 views
0

実際のアプリケーションを作成して自分のapiをラップしようとしていますが、このエラーをデバッグする方法がわかりません。サービス参照サービスでコンストラクタがDIエラーの原因となる

lists.serviceで "private authService"を作成することは、エラーの原因のようです。ログインコンポーネントは正常に動作します。

EXCEPTION: Uncaught (in promise): Error: DI Error Error: DI Error at NoProviderError.ZoneAwareError (http://localhost:4200/polyfills.bundle.js:3270:33) at NoProviderError.BaseError [as constructor] (http://localhost:4200/vendor.bundle.js:29085:16) at NoProviderError.AbstractProviderError [as constructor] (http://localhost:4200/vendor.bundle.js:57951:16) at new NoProviderError (http://localhost:4200/vendor.bundle.js:58013:16) at ReflectiveInjector_._throwOrNull (http://localhost:4200/vendor.bundle.js:78274:19)........

リストをフェッチするサービスを一覧表示します。

import { Injectable } from '@angular/core'; 
import { Http, Headers, RequestOptions, Response } from '@angular/http'; 
import { Observable } from 'rxjs'; 
import 'rxjs/add/operator/map'; 

import { AuthService } from '../auth/auth.service'; 

@Injectable() 
export class ListsService { 

    constructor(private http: Http, private authService: AuthService) { } 

    getLists(): Observable<boolean> { 
    // add jwt token to headers 
    let headers = new Headers({ 'Authorization': 'Bearer ' + this.authService.token }); 
    let options = new RequestOptions({ headers: headers }); 

    return this.http.post('http://localhost:8080/lists', options) 
     .map((response: Response) => response.json()); 
    } 

} 

認証サービスのトークン

import { Injectable } from '@angular/core'; 
import { Http, Headers, Response } from '@angular/http'; 
import { Observable } from 'rxjs'; 
import 'rxjs/add/operator/map' 

@Injectable() 
export class AuthService { 
    public token: string; 

    constructor(private http: Http) { 
    let currentUser = JSON.parse(localStorage.getItem('currentUser')); 
    this.token = currentUser && currentUser.token; 
    } 

    login(email: string, password: string): Observable<boolean> { 
    return this.http.post('http://localhost:8080/login', { email: email, password: password }) 
     .map((response: Response) => { 
     let token = response.json() && response.json().token; 
     if (token) { 
      // set token 
      this.token = token; 
      localStorage.setItem('currentUser', JSON.stringify({ email: email, token: token })); 
      return token; 
     } else { 
      return false; 
     } 
     } 
    ); 
    } 


    logout(): void { 
    this.token = null; 
    localStorage.removeItem('currentUser'); 
    } 

} 
+0

これらのサービスはモジュール内でどこに定義されていますか? – estus

+0

あなたの 'app.module.ts'に' @httpModule'を '@ angular/http'からインポートし、あなたの' AuthService'をプロバイダとして置くことを忘れないでください。あなたが望むのであれば、あなたの 'ListsService'コンストラクタで' @Inject(AuthService)private authService:AuthService'を使うことができます。 –

答えて

1

を得るために、あなたがサービスをしたい場合のように、あなたがアプリでプロバイダとしてそれを含める必要がある他のすべてのサービスが利用できるようにするために、認証など、ルックス.module

関連する問題