1

Using dependency injection like below, I know I can call the translation service. But I have few scenarios where I don't intend to have a constructor-

constructor(
    public translate: TranslateService
  ) { }

Can anyone please suggest if there is a way where I can create a static class and use it like any other normal utility class function? Or if I could somehow get the global reference to translation service without injecting the dependency.

greybeard
  • 2,249
  • 8
  • 30
  • 66
Learner213
  • 13
  • 4
  • Does [importing](https://stackoverflow.com/a/56649754) help? See also: [Don't think so](https://stackoverflow.com/questions/54417060/using-translateservice-inside-a-normal-function-without-class-constructor#comment95644101_54417060). – greybeard Jun 01 '21 at 08:52
  • @greybeard As per the documentation, the only way to work with TranslationService is 1) First to import the service. 2) Then, to inject the dependency in the constructor. My question is for static class, how do I achieve the translation functionality without having dependency injection. – Learner213 Jun 08 '21 at 15:36
  • for normally working app u can't do it. i have expirience with using translateService with inject (custom function in app). after doing this classes will not remove from user memory after descroy component who use this class. if user will get a lot of classes with injected translateService - all will stay in user ram and your service will laggy – Никита Середа Jun 09 '21 at 12:34
  • try to use different, but good way (translate keys into html files). static class will only have some keys and params for it – Никита Середа Jun 09 '21 at 12:35

0 Answers0