17

In a Lightning Component we have the notion of a JavaScript controller and a helper. We can use the helper to call code from the JavaScript controller that is shared between functions, so we can reduce redundancy in the code. This all works great.

However, I was wondering if it is possible, and if so how, to share code between multiple Lightning Components. It doesn't behave like Apex where you can just reference other class.

I was looking at inheritance and abstract components, but I was thinking of code that is shared between components that aren't necessarily related to each other, such as code to handle object and field-level errors.

2 Answers 2

14

Currently the recommended mechanism for this is to use a Static Resource and include it in any components that need to share the same JavaScript via ltng:require (loads the library once and only once similar to requirejs).

4

I'm bringing up this old post in case anyone searches for this. I've created a Service Component pattern. It's working great for one of my production projects, but it still has plenty of room for improvement.

The Service Component pattern has more re-usability and is more akin to having one place to reference an Apex Controller.

https://github.com/tsalb/sfdc-lightning-service-components

You must log in to answer this question.

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