2

I'm coming from AngularJS background, and what I'm really missing in Lightning components is a Service layer. By 'Service' I mean here a singleton class, which could be injected to components, and could be used for sharing data and group reusable functions together.

I noticed that if I inject an external JS into a component, then it is initialized only once per application, even if it is injected to more than one component.

<ltng:require scripts="/resource/singleton_service" afterScriptsLoaded="{!c.scriptLoaded}"/>

--> here c.scriptLoaded is called only once per application.

So, theoretically it can be used to create a global singleton class and use it as a service. Are there any reasons to not to do this?

Are there any other recommended practices to share data between components?

1 Answer 1

3

The closest thing to angular service in lightning is helper method. With Helper methods you can-

  • Keep shared logic at application or component level via application helper method and component helper method respectively.
  • Helper method is use to share code among same instance of component and components related through inheritance.
  • Inherited methods can be overridden in sub components.

What you can't -

  • Store data and share it across all the instance.
  • A helper method shared across all the instances of different components in your application

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .