循环依赖注入 angular 2
Circular dependency injection angular 2
我一直在努力将 services
注入彼此。下面的博客 Circular Dependency in constructors and Dependency Injection 写的
有点让人困惑
One of the two objects is hiding another object C
我在将服务 class 相互注入时出现以下错误
Can't resolve all parameters for PayrollService: (SiteService, StorageService,
SweetAlertService, ?)
//abstractmodal.service.ts
@Injectable()
export abstract class AbstractModel {
abstract collection = [];
constructor(private siteService: SiteService, private storageService: StorageService,
private sweetalertService: SweetAlertService) {}
setCollectionEmpty() {
this.collection = [];
}
}
//account-payable.service.ts
@Injectable()
export class AccountPayableService extends AbstractModel {
public collection = [];
constructor(private sS: SiteService,private stS: StorageService, private sws: SweetAlertService,
private accpPoService: PayablePurchaseOrderService, private attachmentService: AttachmentService,
private injectorService: InjectorService) {
super(sS, stS, sws);
}
}
//injector.service.ts
@Injectable()
export class InjectorService {
constructor(private payrollService: PayrollService) {}
cleanPayrollCollection() {
this.payrollService.setCollectionEmpty();
}
}
//payroll.service.ts
@Injectable()
export class PayrollService extends AbstractModel {
public collection = [];
constructor(private sS: SiteService,private stS: StorageService, private sws: SweetAlertService,
private accpService: AccountPayableService) {
super(sS, stS, sws);
}
}
非常感谢您的评论和回答。
谢谢
您可以通过注入 Injector
而不是导致循环依赖的服务之一来解决循环依赖
private payrollService:PayrollService;
constructor(/*private payrollService:PayrollService*/ injector:Injector) {
setTimeout(() => this.payrollService = injector.get(PayrollService));
}
在我的例子中(Angular 4 在一个 Ionic 项目中)甚至在它被使用之前(在用户交互时)注入服务也不起作用(在启动时相同 "Can't resolve all parameters...")。
对我有用的是按名称注入(并因此提供)服务。
所以在我的应用程序模块中:
...
providers: [{provide: "MyServiceName", MyServiceClass}],
...
需要时:
const myService: MyServiceClass = injector.get("MyServiceName");
...
我知道 post 已经过时了,但还是感谢上面的解决方案对我有用,因为我有循环依赖问题,所以我必须使用注入器来避免创建中介服务来进行通信,尽管上面的解决方案中有一个更正
从Angular5开始我们需要使用:
提供者:[{提供:"MyServiceName",使用类: MyServiceClass}],
然后
injector.get('MyServiceName')
虽然 get 被破坏了
我一直在努力将 services
注入彼此。下面的博客 Circular Dependency in constructors and Dependency Injection 写的
One of the two objects is hiding another object C
我在将服务 class 相互注入时出现以下错误
Can't resolve all parameters for PayrollService: (SiteService, StorageService, SweetAlertService, ?)
//abstractmodal.service.ts
@Injectable()
export abstract class AbstractModel {
abstract collection = [];
constructor(private siteService: SiteService, private storageService: StorageService,
private sweetalertService: SweetAlertService) {}
setCollectionEmpty() {
this.collection = [];
}
}
//account-payable.service.ts
@Injectable()
export class AccountPayableService extends AbstractModel {
public collection = [];
constructor(private sS: SiteService,private stS: StorageService, private sws: SweetAlertService,
private accpPoService: PayablePurchaseOrderService, private attachmentService: AttachmentService,
private injectorService: InjectorService) {
super(sS, stS, sws);
}
}
//injector.service.ts
@Injectable()
export class InjectorService {
constructor(private payrollService: PayrollService) {}
cleanPayrollCollection() {
this.payrollService.setCollectionEmpty();
}
}
//payroll.service.ts
@Injectable()
export class PayrollService extends AbstractModel {
public collection = [];
constructor(private sS: SiteService,private stS: StorageService, private sws: SweetAlertService,
private accpService: AccountPayableService) {
super(sS, stS, sws);
}
}
非常感谢您的评论和回答。
谢谢
您可以通过注入 Injector
而不是导致循环依赖的服务之一来解决循环依赖
private payrollService:PayrollService;
constructor(/*private payrollService:PayrollService*/ injector:Injector) {
setTimeout(() => this.payrollService = injector.get(PayrollService));
}
在我的例子中(Angular 4 在一个 Ionic 项目中)甚至在它被使用之前(在用户交互时)注入服务也不起作用(在启动时相同 "Can't resolve all parameters...")。
对我有用的是按名称注入(并因此提供)服务。
所以在我的应用程序模块中:
...
providers: [{provide: "MyServiceName", MyServiceClass}],
...
需要时:
const myService: MyServiceClass = injector.get("MyServiceName");
...
我知道 post 已经过时了,但还是感谢上面的解决方案对我有用,因为我有循环依赖问题,所以我必须使用注入器来避免创建中介服务来进行通信,尽管上面的解决方案中有一个更正
从Angular5开始我们需要使用:
提供者:[{提供:"MyServiceName",使用类: MyServiceClass}],
然后
injector.get('MyServiceName')
虽然 get 被破坏了