使用同一服务的多个实例 [英] Using multiple instances of the same service

查看:23
本文介绍了使用同一服务的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务,就像这样:

I have a service, like this:

@Injectable()
export class EditorService { ... }

我有一个组件,像这样:

And I have a component, like this:

@Component({
  ...
  template: `<child-component></child-component>`,
  providers: [EditorService],
  ...
})
export class SomeComponent {
  constructor(
    private _appleEditorService: EditorService,
    private _pearEditorService: EditorService) {}
  }

你可能已经注意到,这个组件有一个子组件:

As you may have noticed, this component has a child component:

@Component({
  ...
  selector: 'child-component',
  ...
})
export class ChildComponent {
  constructor(
    private _appleEditorService: EditorService,
    private _pearEditorService: EditorService) {}
  }

如您所见,我想要我的 EditorService 的两个实例:一个用于编辑苹果,另一个用于编辑梨.但是,上面的代码将不起作用,因为 Angular 无法知道 EditorService 的哪个实例是哪个实例,因为它们的变量名称是私有的.ChildComponent 中的 _pearEditorService 也可能是指与 SomeComponent 中的 _appleEditorService 相同的实例.

As you can see, I want two instances of my EditorService: one will be used for editing apples and one for editing pears. However, the code above is not going to work, since there is no way for Angular to know which instance of EditorService is which as their variable names are private. _pearEditorService in ChildComponent might as well be referring to the same instance as _appleEditorService in SomeComponent.

问题:否则,我可以两次使用同一个 Angular2 服务吗?

Question: How else then, can I use the same Angular2 service twice?

我的问题的重点是是否可以使用同一个类.我知道通过为服务的每个实例实际创建一个单独的类,甚至通过继承用很少的代码来做到这一点,有一些解决方法.我只是想知道如果没有它是否可以完成.

The point of my question is if it is possible using the same class. I know there are workarounds by actually creating a seperate class for every instance of the service, and even doing that with little code by inheritance. I just want to know if it can be done without.

推荐答案

Angular DI 为每个提供者维护一个实例.在您的情况下,如果您有两个相同类型的构造函数参数,它们将解析为同一个实例.

Angular DI maintains a single instance per provider. In your case if you have two constructor parameters with the same type, they resolve to the same instance.

你可以做的是提供一个工厂函数(不同于简单的 useFactory 提供者事件,尽管它也使用它)

What you can do is to provide a factory function (different from a simple useFactory provider event though it uses it as well)

(示例复制自 https://stackoverflow.com/a/37517575/217408)

{ provide: EditorService, useFactory: 
    (dep1, dep2) => {
        return (x) => { 
            new EditorService(x, dep1, dep2);
        }
    }, deps: [Dep1, Dep2]
})

....

constructor(@Inject(EditorService) editorServiceFactory: any) {
  let editorService1 = editorServiceFactory(1);
  let editorService2 = editorServiceFactory(2);
}

如果您有固定数量的实例,这应该可行:

If you have a fixed number of instances this should work:

{ provide: 'instance1', useClass: EditorService },
{ provide: 'instance2', useClass: EditorService },

export class SomeComponent {
    constructor(
        @Inject('instance1') private _appleEditorService: EditorService,
        @Inject('instance2') private _pearEditorService: EditorService) {}
}

这篇关于使用同一服务的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆