Wednesday 28 February 2018

What is aura:attribute in lightning components?

What is aura:attribute?
Attributes on components are like instance variables in objects. They’re a way to save values that change, and a way to name those value placeholders. 

Example:
Created Component called AttributeComponent:

  1. <aura:component >
  2.     <aura:attribute name="ProjectName" type="String" required="true"/>
  3.     <aura:attribute name="DefaultView" type="String" default="This is default String"/>
  4.     <aura:attribute name="AccountInfo" type="Account" />
  5.  
  6.     <lightning:card title="Grouped Item">
  7.         <p> Project Name: {!v.ProjectName}</p>
  8.         <p> Default Value: {!v.DefaultView}</p>
  9.         <p> Account Name: {!v.AccountInfo.Name}</p>
  10.         <p> Account Number: {!v.AccountInfo.AccountNumber}</p>
  11.     </lightning:card>
  12.    
  13. </aura:component>

I have created 3 attributes and displayed in the above example.
First Attribute called "ProjectName" and type "String" and required as true.
Second Attribute called "DefaultView" and type "String" with Default value.
Third Attribute called "AccountInfo" and type "Account" (Standard Object).

Created another Component called PassAttributeValue:

  1. <aura:component >
  2.     <c:AttributeComponent ProjectName="My Project" AccountInfo="{'sobjectType': 'Account', 'Name' : 'Test Account', 'AccountNumber' : '322342'}"/>
  3. </aura:component>

In this component, I am passing/assigning values to the attributes. So if I run PassAttributeValue component via lightning app, then output will be as below,



Attribute Data Types:
aura:attribute type supports the following data types,
  • Primitives data types, such as Boolean, Date, DateTime, Decimal, Double, Integer, Long, or String. The usual suspects in any programming language.
  • Standard and custom Salesforce objects, such as Account or MyCustomObject__c.
  • Collections, such as List, Map, and Set.
  • Custom Apex classes.
  • Framework-specific types, such as Aura.Component, or Aura.Component[]. These are more advanced than we’ll get to in this module, but you should know they exist.

Reference:

No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...