【发布时间】:2018-02-10 07:51:37
【问题描述】:
我已开始在 Angular 5 上构建应用程序,但在将服务注入另一个服务时遇到错误问题。
下面是我的数据服务
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { environment } from '../../../environments/environment';
import { AuthenticationService } from '../../services/authentication/authentication.service';
@Injectable()
export class DataService {
private headers;
public user: any;
constructor(
private http: HttpClient,
private auth: AuthenticationService
) {
if (auth.loggedIn) {
this.setHeader();
}
}
}
身份验证服务:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { RouterModule, Router } from '@angular/router';
import { environment } from '../../../environments/environment';
import { error } from 'selenium-webdriver';
import { DataService } from '../../services/dataservice/data.service';
@Injectable()
export class AuthenticationService {
public loggedIn: boolean;
constructor(
private http: HttpClient,
private dataservice: DataService,
private router: Router
) { }
}
我在这里注入另一个自定义服务,即 AuthenticationService。
但是,在运行时它给了我一个错误,我无法弄清楚它为什么会抛出错误:
Uncaught Error: Can't resolve all parameters for DataService: ([object Object], ?).
已在我的 app.module.ts 中注入提供程序:
providers: [
DataService,
AuthenticationService,
AuthGuard
],
提前致谢。
【问题讨论】:
-
你能发布认证服务吗
-
现在也发布了认证服务.....
-
你需要解决循环依赖
标签: angular dependency-injection frontend angular-services