简体   繁体   English

如何在 angular 8 中使用注入器注入使用注入令牌的服务

[英]How to inject service that is using injection token using injector in angular 8

I'm developing a package that is used by two projects in my company.我正在开发我公司的两个项目使用的 package。 In project A and only there we got a message service that is provided in core.module like this:在项目 A 中,只有我们在core.module中提供了一个消息服务,如下所示:

        {
          provide: 'MESSAGE_SERVICE',
          useClass: MessageService
        }

In my component, I need to inject this service.在我的组件中,我需要注入此服务。 I'm doing it using injector .我正在使用injector来做这件事。 However the method get that accepts a string as a parameter is deprecated so I cannot use it, although it works.但是,接受字符串作为参数的方法get已被弃用,因此我无法使用它,尽管它有效。 When I try to use a new API with InjectionToken I always get undefined.当我尝试使用带有InjectionToken的新 API 时,我总是不确定。 Here's my code:这是我的代码:

  protected messageService: any;

  constructor(protected injector: Injector) {
    const messageServiceInjectionToken = new InjectionToken('MESSAGE_SERVICE');
     // i get null
    this.messageService = this.injector.get<any>(messageServiceInjectionToken, null, InjectFlags.Optional);
    // It works but method is deprecated
    this.messageService = this.injector.get('MESSAGE_SERVICE', null);
    // It works but I cannot use MessageService class directly as it is not present in application B
    this.messageService = this.injector.get(MessageService);
  }

What is the proper way to get my service without using direct imports of said class?在不使用直接进口的 class 的情况下获得我的服务的正确方法是什么? I thought of creating a mock class in project B so I would be able to use MessageService class.我想在项目 B 中创建一个模拟 class,这样我就可以使用MessageService class。 However, I'd like to know if there's a better way.但是,我想知道是否有更好的方法。

EDIT编辑

The shared service is just a base class to other implementations, it is provided using a factory so there's not dependency injection in it.共享服务只是其他实现的基础 class,它是使用工厂提供的,因此其中没有依赖注入。 This is how it is provided:它是这样提供的:


export function parentServiceFactory(platformService: PlatformService, injector: Injector) {
  if (platformService.isIOS) {
    return new IosChildService(injector);
  }

  if (platformService.isAndroid) {
    return new AndroidChildService(injector);
  }

  return new DesktopChildService(injector);
}

@NgModule({
providers: [
    {
      provide: ParentService,
      useFactory: parentServiceFactory,
      deps: [PlatformService, Injector]
    }
]
})
export class SharedModule {}

The purpose of that injection token is to be able to not use the injector.该注入令牌的目的是能够不使用注入器。

Instead =, use this syntax而是 =,使用此语法

constructor(@Inject('MESSAGE_SERVICE')protected service: YourInterface)

Although if you make a service, I don't see the point of using an injection token.尽管如果您提供服务,我看不出使用注入令牌的意义。

You can create an angular library and then use it in any number of projects您可以创建一个 angular 库,然后在任意数量的项目中使用它

Here's how to create an angular2 library https://makina-corpus.com/blog/metier/2017/how-to-create-an-angular-library以下是如何创建 angular2 库https://makina-corpus.com/blog/metier/2017/how-to-create-an-angular-library

with angular-cli https://medium.com/@nikolasleblanc/building-an-angular-4-component-library-with-the-angular-cli-and-ng-packagr-53b2ade0701e使用 angular-cli https://medium.com/@nikolasleblanc/building-an-angular-4-component-library-with-the-angular-cli-and-ng-packagr-53b2ade0701e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM