在我的角度应用程序中创建服务的双重实例 [英] Create a double instance of a service in my angular app

查看:59
本文介绍了在我的角度应用程序中创建服务的双重实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通用服务.我想使用不同的类型=>

I have a Generic service. I would like to create two instance of this service with different type =>

constructor(
  private mapSynchService: 
SynchronizatorService<SynchMapElement, SynchMapElementSubject>,
  private waypointSynchService: 
SynchronizatorService<SynchWaypointElement, SynchWaypointElementSubject>) { }

问题,我认为这会创建两个不同的服务,原因是定义不同,但事实并非如此.但是我不太了解如何使这些服务在我的所有模块中可用,以及如何为需要服务1的组件提供正确的服务,以及为需要服务2的组件提供正确的服务.

The problem, I thought this would create two different service cause the definition is different, but it is not doing so. But I don't quite understand how I can make those service available in all my module, and get the correct one for the component that need service 1, and same for those who need service 2.

我看到了一些有关注入令牌的文章,但是我的实现失败了.我在模块中尝试了类似的方法

I have seen some post about injection token, but my implementation failed. I tried something like this in the module

providers: 
  [{provide: 
    new InjectionToken<SynchronizatorService<SynchWaypointElement, SynchWaypointElementSubject>>()}]

但是有很多错误

推荐答案

您将需要两个单独的注入令牌来提供单独的实例.

You will need two separate injection tokens for providing separate instances.

每个令牌的工作方式类似于Angular DI容器到值/类/工厂映射的唯一名称.还要确保该服务标记为@Injectable,以便Angular能够识别它.

Each token works like a unique name to value/class/factory mapping for the Angular DI container. Also make sure the service is marked @Injectable for Angular to recognise it.

示例:


// Export unique Injection Token

export const SYNCH_MAP = new InjectionToken<SynchronizatorService<SynchMapElement, SynchMapElementSubject>>('Token for SynchMap');
export const SYNCH_WAYPOINT = new InjectionToken<SynchronizatorService<SynchWaypointElement, SynchWaypointElementSubject>>('Token for SynchWaypoint');

// Using at a module level

@NgModule({
    ...
    providers: [
        { provide: SYNCH_MAP, useClass: SynchronizatorService<SynchMapElement, SynchMapElementSubject> }
        { provide: SYNCH_WAYPOINT, useClass: SynchronizatorService<SynchMapElement, SynchMapElementSubject> }
    ]
})


// Injecting in a component 

constructor(
    @Inject(SYNCH_MAP) private instance1: SynchronizatorService<SynchMapElement, SynchMapElementSubject>,
    @Inject(SYNCH_WAYPOINT) private instance2: SynchronizatorService<SynchMapElement, SynchMapElementSubject>,
)

IMO,最好使用useFactory提供程序来更好地控制初始化服务实例.我假设您的服务定义正在使用模板<T>.请查看 https://angular.io/guide/dependency-injection-providers#predefined-tokens-and-multiple-providers

IMO, it would be better to use a useFactory provider for having more control on initialising the service instances. As I assume your service definition is using a template <T>. Please have a look at https://angular.io/guide/dependency-injection-providers#predefined-tokens-and-multiple-providers

这篇关于在我的角度应用程序中创建服务的双重实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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