When to set Lightning access=GLOBAL in Managed Packages
Let's consider the following base scenario to answer your questions:
- you plan to use a
contrib:parentComp
component. contrib:parentComp
has a child componentcontrib:childComp
.contrib:childComp
has 3 attributesglobalAttr
,publicAttr
andprivateAttr
with the 'hinted' access levels.- the
contrib
namespace does not belong to your org. - your org has your own
myns
customized namespace (it could also be the defaultc
namespace as long as it differs fromcontrib
).
Back to your questions, here are the answers:
Only the
parentComp
component needs to beglobal
if your want to use it in themyns
namespace.- If
childComp
ispublic
you cannot directly call it as a standalone in yourmyns
code butparentComp
will still work as expected. - If
childComp
isglobal
you could call it directly in yourmyns
code without usingparentComp
.
- If
The same rule applies to
aura:attribute
. Let's consider for this answer thatchildComp
isglobal
.- If you use it in
myns
code, you can only accessglobalAttr
. - If you use it in
contrib
code (in the org where the component was implemented) then, you can accessglobalAttr
andpublicAttr
when declaring an instance of the component.
- If you use it in
private
attributes are used to hold internal component state that will not be shared with a parent component regardless of the namespace.
I hope that this clarifies the use of the different access levels.
The Developer's Blog on How to Design, Build and Publish Your Lightning Component for AppExchange states the following:
Development best practice is to specify access attributes manually from the start. If you are planning to contribute an application or component, remember to always use “global” access to expose functionalities.
For point number 1, as a best practice, always set component level access to GLOBAL if you wish to expose them.
For point 2, if you plan on making the attributes accessible through the .design file, you will need to set access to GLOBAL.
For point 3, It is recommended to set them to private if you don't plan on making them accessible by other components outside of the attribute’s namespace.
As mentioned in Attribute Access control and in Modularizing Code in Lightning Components
Both articles are worth reading.
Generally speaking:
- Global: Any component can access the value of this attribute.
- Public: Only components in the same namespace can set the value of this attribute.
- Private: Only this component can set the value of this attribute.