在服务注入错误中注入服务(循环依赖注入)
Injection of service in service giving error (Circular dependency injection)
我已经开始在 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
],
提前致谢。
看到你的代码后你在 DataService 和 AuthenticationService 之间有一个循环依赖,这不可能用构造函数注入
要解决这个问题,您可以这样做
export class AuthenticationService {
public token: any;
dataService: DataService;
constructor(
private http: Http,
injector:Injector;
private router: Router
) {
setTimeout(() => this.dataService= injector.get(DataService));
}
我已经开始在 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
],
提前致谢。
看到你的代码后你在 DataService 和 AuthenticationService 之间有一个循环依赖,这不可能用构造函数注入
要解决这个问题,您可以这样做
export class AuthenticationService {
public token: any;
dataService: DataService;
constructor(
private http: Http,
injector:Injector;
private router: Router
) {
setTimeout(() => this.dataService= injector.get(DataService));
}