服务引用服务,构造函数导致DI错误

Service referencing service, constructor causes DI error

正在尝试创建一个真实世界的应用程序来包装我的 api,但我不确定如何调试此错误。

在 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');
  }

}

看起来如果您希望某个服务(例如 auth)可用于所有其他服务,您需要将其作为提供者包含在 app.module