使用同一服务的多个实例

使用同一服务的多个实例

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

问题描述

我有这样的服务:

@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) {}
}

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

08-07 05:07