1

When using the vueJs, we can combine all into one repository, and call it like this.$service1, this.$service2

But for the angular, if want to call a service, need to initial inside the constructor. If i have multiple, then will need to do many time.

Is that any way that can combine all into one file or js file, then we can call it without initial inside the constructor of each component?

Ben
  • 443
  • 4
  • 16
  • 1
    You can always make a proxy service where you initialize all your services and delegate the calls, but that is more work than just putting the service in the constructor. Also you would have to put that proxy service in the constructor anyways. – Ruben Szekér Aug 11 '20 at 07:20
  • @RubenSzekér means that, it cannot have the similar function like the vuejs did? Based on what you explained, this still need to initial in constructor, i think no big difference. Can still continue to use the exisiting method – Ben Aug 11 '20 at 07:46
  • 1
    You will atleast one injection somewhere, either the component where you use it, a proxy, etc. [This stackoverflow post](https://stackoverflow.com/questions/37482460/getting-instance-of-service-without-constructor-injection) explains how you can create global variables so you don't need dependency injection in the component where you use it but you will need dependency injection atleast in one location. – Ruben Szekér Aug 11 '20 at 09:09

0 Answers0