+ Start a Discussion
Artur Kolasa 7Artur Kolasa 7 

What is correct approach to use constants in lightning components?

Hi
I have problem in my project, because i have application components with a lot of components and i have to define some constance to use them across all components in application. What is the best approach for this? Currently I define a lot of attributes with default text value and I use them as constans but i think that isn't the best solution.
 
Best Answer chosen by Artur Kolasa 7
RahulForceRahulForce
Hi Artur,
What you can do is use a single attribute of type object and then you can have sub attributes for that object type.
 
<aura:attribute name="myObj" type="Object" 
                default="{
                         'sobjectType': 'Object',                                                                                  
                         'attr1': 'ee',
                         'attr2': 'ee',
                         'attr3': 'ee',
                         'attr4': 'eerf',
                         'attr5': 'ef',
                         'attr6': 'sas',
                         }"/>

<ui:Text aura:id="myid" label="name" value="{!v.myObj.attr1}" />
You can use it like this

All Answers

RahulForceRahulForce
Hi Artur,
What you can do is use a single attribute of type object and then you can have sub attributes for that object type.
 
<aura:attribute name="myObj" type="Object" 
                default="{
                         'sobjectType': 'Object',                                                                                  
                         'attr1': 'ee',
                         'attr2': 'ee',
                         'attr3': 'ee',
                         'attr4': 'eerf',
                         'attr5': 'ef',
                         'attr6': 'sas',
                         }"/>

<ui:Text aura:id="myid" label="name" value="{!v.myObj.attr1}" />
You can use it like this
This was selected as the best answer
Lalit Mistry 21Lalit Mistry 21
Hi Artur,

You can create a custom label and refer as {!$Label.c.LabelApiName} in component or  $A.get("$Label.c.LabelApiName") in your controller or helper.
Mark this as an answer if it solves your problem.
Artur Kolasa 7Artur Kolasa 7
Thanks for yours reply :) I think that one attribute with sub attributes is pretty good solution but Salesforce should give some better opportunity for constants in lightening.