Friday, 17 January 2020

How to declare different types of attributes in component

<aura:component >
    
    <!--Basic types-->
    <aura:attribute name="ItemName" type="String" default="Bag"/>
    <aura:attribute name="SerialNumber" type="Long" default="1"/>
    <aura:attribute name="Quantity" type="Integer" default="2"/>
    <aura:attribute name="Price" type="Decimal" default="4578.67"/>
    <aura:attribute name="PurchaseDate" type="Date" default="2018-11-9"/>
    <aura:attribute name="Dispatched" type="Boolean" default="true"/>
    <aura:attribute name="Color" type="String" default="blue"/>
    
    <!--Collection types-->
    <aura:attribute name="ColorArray" type="String[]" default="['red', 'green', 'blue']"/>
    <aura:attribute name="ColorList" type="List" default="['red', 'green', 'blue']"/>
    <aura:attribute name="ColorMap" type="Map" default="{a: 'red', b: 'green', c: 'blue'}"/>
    <aura:attribute name="ColorSet" type="Set" default="['red', 'green', 'blue','red']"/>
    
    <!--Object types-->
    <aura:attribute name="PersonObject" type="Object" default="{'name': 'John', 'age': '25'}"/>
    
    <p>Your component code goes here.</p>
    
</aura:component>



Note :  The type value of an attribute is case insensitive, and also when you use a particular attribute in an expression the name of the attribute should match with the name specified in attribute definition i.e. the name inside expressions is case sensitive.

Parameters of attribute:

Access : Indicates whether the attribute can be used outside of its own namespace. Possible values are public (default), and global, and private.
Name : The name of the attribute. It is required and must be provided.
Type  : Type of the attribute. It could be any of the type that we discussed above like String, Boolean etc.
Default : The default value that you want to provide to the attribute.
Required : It contains boolean value true or false.  The default value is false.

Description : A brief summary of the attribute and its usage.

No comments:

Post a Comment

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...