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:
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.
Created another Component called PassAttributeValue:
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:
- <aura:component >
- <aura:attribute name="ProjectName" type="String" required="true"/>
- <aura:attribute name="DefaultView" type="String" default="This is default String"/>
- <aura:attribute name="AccountInfo" type="Account" />
- <lightning:card title="Grouped Item">
- <p> Project Name: {!v.ProjectName}</p>
- <p> Default Value: {!v.DefaultView}</p>
- <p> Account Name: {!v.AccountInfo.Name}</p>
- <p> Account Number: {!v.AccountInfo.AccountNumber}</p>
- </lightning:card>
- </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).
- <aura:component >
- <c:AttributeComponent ProjectName="My Project" AccountInfo="{'sobjectType': 'Account', 'Name' : 'Test Account', 'AccountNumber' : '322342'}"/>
- </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