很喜歡曾經看到的一句話:以輸出倒逼輸入。以輸出的形式強制本身學習,確實是高效的學習方式,真的很棒。如下僅爲我的學習理解,若有錯誤,歡迎指出,共同窗習。javascript
Lightning Component框架是一個UI框架,用於爲移動和臺式設備開發Web應用程序。這是一個單頁面Web應用框架,用於爲Lightning Platform應用程序構建具備動態,響應式用戶界面的單頁應用程序。它在客戶端使用JavaScript,在服務器端使用Apex。css
Lightning Component做爲Web應用框架,能夠輕鬆的建立自定義應用程序,而沒必要本身編寫所有代碼。經常使用的Web應用程序框架有不少, 例如:Ruby on Rails, Grails, AngularJS, Django, CakePHP等等,甚至前面提到的Visualforc component也是一種web框架。java
固然,Lighnting Component也能夠當作是CS架構。web
客戶端主要由兩個元素組成:View和Controller數據庫
服務端主要也有兩個元素組成:Controller和Database。express
Aura組件即是基於Lighnting Component框架進行的二次開發。數組
建立組件時,其命名的規則必須知足以下條件:服務器
當咱們在工程中建立一個新的Aura捆綁包(如下捆綁包都稱爲組件)時,工程會自動建立出.auradoc,.cmp,.xml,.css,.design,svg,controller.js,help.js,renderer.js幾個文件。架構
資源 | 資源名稱 | 用途 |
component | testAura.cmp | 在捆綁包中惟一的必要資源,包含了組件的標記,而且每一個捆綁包中只有一個component。 |
CSS樣式 | testAura.css | 組件的形狀格式 |
Design | testAura.design | 當組件在Lightning App Builder或者Lightning Page中使用時須要用到該文件 |
Helper | testAuraHelper.js | Javascript函數,該函數能夠被捆綁包中的任何javascript代碼調用 |
Documentation | testAura.auradoc | 對組件的一些簡單介紹說明 |
Renderer | testAuraRenderer.js | 客戶端渲染器會覆蓋默認的渲染器 |
Controller | testAuraController.js | 客戶端的控制函數,用來處理組件中的事件 |
SVG文件 | testAura.svg | 組件的自定義圖標資源,通常爲Lightning App Builder中使用的圖標 |
組件捆綁包中的全部資源都遵循命名規則,而且自動綁定鏈接。例如:<aura:component controller="TestAuraController">,組件會自動鏈接TestAuraController.cls類,因此組件內全部資源均可鏈接該控制類。app
組件由自動建立的一系列文件組成,而且每一個文件都發揮着不一樣的功能。其中,組件的主體即是component(.cmp文件),對於最精簡的組件來說,只修改.cmp文件便可(其餘文件使用默認值),該文件爲組件定義了視圖。
固然,對於實際項目開發中,僅僅含有視圖是遠遠不夠的。一般,咱們須要修改controller.js和helper.js文件。controller.js與.cmp文件交互,提供視圖中所須要的數據;helper.js與服務器controller.cls交互,獲取數據庫中的數據;controller.js直接調用helper.js中的函數(固然,能夠把helper.js中的函數直接寫在controller.js中,直接從controller.js中獲取數據庫中的數據,但該模式不便與維護,不推薦使用)。
若是組件須要與服務器中數據庫進行交互,則還需建立一個Apex控制類,與控制類與數據庫交互,並將數據傳遞給組件。
不一樣文件之間的聯繫,以下圖所示:
每一個組件都是命名空間的一部分,若是Org中設置了命名空間前綴,那麼需使用該命名空間訪問組件。不然,使用默認命名空間訪問組件,系統默認的命名空間爲「c」。
命名規則必須知足如下條件:
例如: myNamespace123和my_namespace是有效的;123MyNamespce和my__namespace是無效的。
Setup-->Packages(注意:該條目只在Salesforce Classic版本中才有)-->Developer Settings-->Edit
Check Avaliability校驗名稱是否知足規則。
引用項 | 示例 |
標記中使用組件 | <c:myComponent> |
系統屬性中使用組件 | <aura:component extends="c:myComponent"> |
Apex控制類 | <aura:component controller="ExpenseController"> |
屬性的類型爲自定義對象 | <aura:attribute name="expense" type="Expense__c" /> |
屬性的類型爲自定義對象,而且設置默認值 |
<aura:attribute name="newExpense" type="Expense__c" default="{ 'sobjectType': 'Expense__c', 'Name': '', 'Amount__c': 0, … }" /> |
表達式中含有自定義對象的字段 | <ui:inputNumber value="{!v.newExpense.Amount__c}" label=… /> |
javascript函數中含有自定義對象字段 |
updateTotal: function(component) { … for(var i = 0 ; i < expenses.length ; i++){ var exp = expenses[i]; total += exp.Amount__c; } … } |
在Javascript函數中動態建立新的組件 |
var myCmp = $A.createComponent("c:myComponent", {},
function(myCmp) { }
);
|
在Javascript函數中的接口對比 | aCmp.isInstanceOf("c:myInterface") |
註冊事件 | <aura:registerEvent type="c:updateExpenseItem" name=… /> |
事件處理 | <aura:handler event="c:updateExpenseItem" action=… /> |
顯式依賴 | <aura:dependency resource="markup://c:myComponent" /> |
Javascript函數中的應用事件 | var updateEvent = $A.get("e.c:updateExpenseItem"); |
靜態資源 | <ltng:require scripts="{!$Resource.resourceName}" styles="{!$Resource.resourceName}" /> |
引用項 | 示例 |
標記中使用組件 | <yournamespace:myComponent /> |
系統屬性中使用組件 | <aura:component extends="yournamespace:myComponent"> |
Apex控制類 | <aura:component controller="yournamespace.ExpenseController"> |
屬性的類型爲自定義對象 | <aura:attribute name="expenses" type="yournamespace__Expense__c[]" /> |
屬性的類型爲自定義對象,而且設置默認值 |
<aura:attribute name="newExpense" type="yournamespace__Expense__c" default="{ 'sobjectType': 'yournamespace__Expense__c', 'Name': '', 'yournamespace__Amount__c': 0, … }" /> |
表達式中含有自定義對象的字段 | <ui:inputNumber value="{!v.newExpense.yournamespace__Amount__c}" label=… /> |
javascript函數中含有自定義對象字段 |
updateTotal: function(component) { … for(var i = 0 ; i < expenses.length ; i++){ var exp = expenses[i]; total += exp.yournamespace__Amount__c; } … } |
在Javascript函數中動態建立新的組件 |
var myCmp = $A.createComponent("yournamespace:myComponent",
{},
function(myCmp) { }
);
|
在Javascript函數中的接口對比 | aCmp.isInstanceOf("yournamespace:myInterface") |
註冊事件 | <aura:registerEvent type="yournamespace:updateExpenseItem" name=… /> |
事件處理 | <aura:handler event="yournamespace:updateExpenseItem" action=… /> |
顯式依賴 | <aura:dependency resource="markup://yournamespace:myComponent" /> |
Javascript函數中的應用事件 | var updateEvent = $A.get("e.yournamespace:updateExpenseItem"); |
靜態資源 | <ltng:require scripts="{!$Resource.yournamespace__resourceName}" styles="{!$Resource.yournamespace__resourceName}" /> |
在建立Aura組件時,可在該文件中配置組件的配置選項。配置選項都是可選的,因此能夠進行任意組合。
在Aura組件中提供以下配置項:
配置 | 標記 | 描述 |
Lightning Tab | implements="force:appHostable" | 建立一個組件,該組件能夠做用Lightning Experience或者Salesfroce手機App的導航元素 |
Lightning Page | implements="flexipage:avaliableForAllPageTypes" and access="global" | 建立一個組件,該組件能夠用在Lightning頁面或者Lightning App Builder中 |
Lighnting Record Page | implements="flexipage:availableForRecordHome, force:hasRecordId" and access="global" | 建立一個組件,該組件能夠用在Lightning Experience的記錄的Home頁面 |
Lighnting Communities Page | implements="forceCommunity:availableForAllPageTypes" and access="global" |
建立一個組件,該組件支持在Community Builder中拖拽功能 |
Lighnting Quick Action | implements="force:lightningQuickAction" | 建立一個組件,該組件能夠充當一個Lightnging quick action |
示例:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes"> </aura:component>
組件屬性相似與Apex中類的成員變量(或者說Java中類的成員變量)。他們是組件在特定的實例上設置的類型化字段,可使用表達式語法從組件的標記內引用他們。
語法:<aura:attribute name="**" type="**" default="**" required="true/false" access="**" description="**">
示例:
<aura:component> <aura:attribute name="whom" type="String" default="world"/> Hello {!v.whom}! </aura:component>
1) 屬性命名規則:
2) 屬性type支持的類型
aura:attribute支持的類型有如下幾種:基礎類型,函數類型,對象類型,標準和自定義對象類型,集合類型,Apex Class類型,指定框架類型。
類型 | 示例 | 描述 |
Boolean | <aura:attribute name="showDetail" type="Boolean" /> | 值爲true/false |
Date | <aura:attribute name="startDate" type="Date" /> | 日期類型,格式爲:yyyy-mm-dd。hh:mm:ss沒有保存。 |
DateTime | <aura:attribute name="lastModifiedDate" type="DateTime" /> | 日期類型,對應時間戳格式。 保存了除了日期,還保存了時間,而且精確到毫秒。 |
Decimal | <aura:attribute name="totalPrice" type="Decimal" /> | 十進制,能夠包括小數部分。對應Java.math.BigDecimal,精度高於Double類型。 針對貨幣字段,通常選擇該類型。 |
Double | <aura:attribute name="widthInchesFractional" type="Double" /> | Double類型,能夠包含小數位。對應Java.lang.Double。 |
Integer | <aura:attribute name="numRecords" type="Integer" /> | 整數類型,不包含小數位。對應Java.lang.Integer。 |
Long | <aura:attribute name="numSwissBankAccount" type="Long" /> | 長整型,不包含小數位。對應Java.lang.Long。 |
String | <aura:attribute name="message" type="String" /> | 字符串類型。 |
示例:
<aura:attribute name="favoriteColors" type="String[]" default="['red','green','blue']" />
屬性的類型能夠對象Javascript中的某個函數。若是子組件具備該類型的屬性,可傳遞迴調函數給父組件。
示例:
<aura:attribute name="callback" type="Function" />
注意:該類型不適用於服務端,僅在客戶端使用。
該類型的屬性對應一個對象。
示例:
<aura:attribute name="data" type="Object" />
注意:通常狀況下,不建議使用該類型。object類型的屬性在傳遞至服務端時,會將全部的東西序列化爲字符串,此時若是使用深度表達(例如:v.data.property),則會拋出字符串沒有該屬性異常。所以,儘可能使用type="Map",防止出現反序列化等問題。
屬性支持標準或自定義對象的類型。
示例:
<aura:attribute name="acct" type="Account" /> <aura:attribute name="expense" type="Expense__c" />
注意:用戶至少對該對象具備讀取權限,不然組件不會加載。
下面爲支持的集合類型:
類型 | 示例 | 描述 |
type[](Array) | <aura:attribute name="colorPalette" type="String[]" default="['red', 'green', 'blue']" /> | 自定義數組 |
List | <aura:attribute name="colorPalette" type="List" default="['red', 'green', 'blue']" /> | 有序的列表 |
Map | <aura:attribute name="sectionLabels" type="Map" default="{ a: 'label1', b: 'label2' }" /> | key:value集合。key不可重複。 若是不設置默認值,則在Javascript中默認設爲null。 若是想設置空值,可寫爲:default="{}" |
Set | <aura:attribute name="collection" type="Set" default="['red', 'green', 'blue']" /> | 集合,無序,不含重複元素。 |
示例:
<aura:component controller="TestAuraController" access="global"> <aura:attribute name="selectedDisplayMonth" type="String" default="6 Months,12 Months,18 Months,24 Months,36 Months" /> <aura:iteration items="{!v.displayMonths}" var="displayMonth"> {!displayMonth} </aura:iteration> </aura:component >
該類型屬性對應一個Apex類。
示例:
存在某個自定義Apex類:TestAura、
<aura:attribute name="color" type="docSampleNamespace.TestAura" />
下面爲支持的指定框架類型:
類型 | 示例 | 描述 |
Aura:component | N/A | 一個單獨的組件。 相比較而言,官方推薦使用Aura:component[]類型。 |
Aura:component[] |
<aura:component> <aura:attribute name="detail" type="Aura.Component[]"> <p>default paragraph1</p> </aura:attribute> Default value is: {!v.detail} </aura:component> |
利用該類型能夠設置一個類型塊。 |
Aura.Action | <aura:attribute name =「 onclick」 type =「 Aura.Action」 /> | 使用此類型,能夠將action傳遞給組件。 |
在3.4.2的組件屬性示例中,新建了一個屬性whom, 引用該屬性時使用了表達式:{!v.whom},負責該屬性的動態輸出。
語法:{!expression}
上述示例中,咱們的屬性名稱定義爲whom,v表示視圖(View)。當組件使用時,表達式的值將被評估而且動態替換。
注意:表達式區分大小寫。空格忽略。若是自定義字段爲myNamespace__Amount__c,要獲取該屬性值,必須寫爲:{!v.myObject.myNamespace__Amount__c}
1) 表達式動態輸出
利用表達式是最簡單的值動態輸出方式。
表達式的值能夠來自:component屬性,具體的數字,布爾值等。
示例:
component屬性:{!v.whom} ==> 輸出屬性名爲whom的值
文字值:{!123}, {!'abc'} ==> 輸出分別爲:123, abc
布爾值:{!true}, {!false} ==> 輸出分別爲:true,false
注意:文字值中,「!」後面能夠直接跟數字值,若是是字符則須要用單引號' '包起來,不包含則組件不會加載,用雙引號會報錯。
2) 條件表達式
與全部語言同樣,這裏也支持三元表達式,想必你們對三元表達式的概念都很清楚,這裏就再也不解釋了。
示例:
{!v.displayMonth == '' ? 'No value' : 'Has value'}
displayMonth屬性值不爲空字符,打印:Has value;
displayMonth屬性值爲空字符,打印:No value
相似與Java中if-else
示例:
<aura:component> <aura:attribute name="read" type="Boolean" default="false" /> <aura:if isTrue="{!v.read}"> you can read it. <aura:set attribute="else"> you cannot read it. </aura:set> </aura:if> </aura:component>
read屬性值爲:true,打印:you can read it.
read屬性值爲:false,打印:you cannot read it.
3) 不一樣組件間數據綁定
當咱們在在一個View中添加另外一個組件,能夠在父組件中初始化子組件的屬性值。目前有兩種語法格式:
語法1: <c:childComponent childAttr="{!v.parentAttr}" />
綁定語法,將父組件中的parentAttr屬性和子組件的childAttr屬性關聯,初始化時將parentAttr的值傳遞給childAttr。運行中修改任意一個屬性,都會致使另一個屬性值的改變。
示例:
parentAura.cmp
<!--Parent component--> <aura:component access="global"> <aura:attribute name="parentAttr" type="String" default="Parent Attribute" /> <!--實例化childAura組件--> <c:childAura childAttr="{!v.parentAttr}" /> <br/> parentAttr in parent: {!v.parentAttr} <div style="background:white"> <lightning:button label="Apply" onclick="{!c.applyHandle}" disabled="false" /> </div> </aura:component>
parentAuraController.js
({ applyHandle: function (cmp, event, helper) { cmp.set('v.parentAttr', 'Parent update'); } })
childAura.cmp
<!--Child component--> <aura:component> <aura:attribute name="childAttr" type="String" default="Child Attribute"/> <div class="slds-p-top--large" tyle="background:white"> childAttr in child: {!v.childAttr} <lightning:button label="Apply" onclick="{!c.applyHandle}" disabled="false" /> </div> </aura:component>
childAuraController.js
({ applyHandle : function(component, event, helper) { component.set('v.childAttr', 'Child update'); } })
output:
childAttr in child: Parent Attribute parentAttr in parent: Parent Attribute
點擊childAura組件的apply按鈕
childAttr in child: Child update parentAttr in parent: Child update
點擊parentAura組件的apply按鈕
childAttr in child: Parent update parentAttr in parent: Parent update
語法2: <c:childComponent childAttr="{#v.parentAttr}" />
非綁定語法,將父組件中的parentAttr屬性和子組件的childAttr屬性關聯,初始化時將parentAttr的值傳遞給childAttr。運行中修改任意一個屬性,只改變當前屬性值,不會修改另一個屬性值。
示例:
parentAura.cmp
<!--Parent component--> <aura:component access="global"> <aura:attribute name="parentAttr" type="String" default="Parent Attribute" /> <!--實例化childAura組件--> <c:childAura childAttr="{#v.parentAttr}" /> <br/> parentAttr in parent: {!v.parentAttr} <div style="background:white"> <lightning:button label="Apply" onclick="{!c.applyHandle}" disabled="false" /> </div> </aura:component>
parentAuraController.js
({ applyHandle: function (cmp, event, helper) { cmp.set('v.parentAttr', 'Parent update'); } })
childAura.cmp
<!--Child component--> <aura:component> <aura:attribute name="childAttr" type="String" default="Child Attribute"/> <div class="slds-p-top--large" tyle="background:white"> childAttr in child: {!v.childAttr} <lightning:button label="Apply" onclick="{!c.applyHandle}" disabled="false" /> </div> </aura:component>
childAuraController.js
({ applyHandle : function(component, event, helper) { component.set('v.childAttr', 'Child update'); } })
output:
childAttr in child: Parent Attribute parentAttr in parent: Parent Attribute 點擊childAura組件的apply按鈕 childAttr in child: Child update parentAttr in parent: Parent Attribute 點擊parentAura組件的apply按鈕 childAttr in child: Child update parentAttr in parent: Parent update
Aura框架能夠經過access屬性自由的控制應用,組件,屬性,接口,事件,方法的訪問權限。access屬性指定資源是否能夠被所在命名空間以外使用。
使用範圍
可在以下tag中使用access屬性:
access的值
應用訪問控制
access在aura:application標籤中控制app是否可被所在命名控制以外訪問。
修飾符 | 描述 |
public | 僅在當前org中可用。access的默認值。 |
global | 在全部的org中可用。 |
組件訪問控制
access在aura:component標籤中控制app是否可被所在命名控制以外訪問。
修飾符 | 描述 |
public | 僅在當前org中可用。access的默認值。 |
global | 在全部的org中可用。 |
屬性訪問控制
access在aura:attribute標籤中控制app是否可被所在命名控制以外訪問。
修飾符 | 描述 |
private | 可在應用,組件,接口,事件或者方法中使用, 不能被外部資源使用(命名空間) |
public | 僅在當前org中可用。access的默認值。 |
global | 在全部的org中可用。 |
接口訪問控制
access在aura:interface標籤中控制app是否可被所在命名控制以外訪問。
修飾符 | 描述 |
public | 僅在當前org中可用。access的默認值。 |
global | 在全部的org中可用。 |
事件訪問控制
access在aura:event標籤中控制app是否可被所在命名控制以外訪問。
修飾符 | 描述 |
public | 僅在當前org中可用。access的默認值。 |
global | 在全部的org中可用。 |
示例:
<aura:component access="global">
...
</aura:component>
在捆綁包中,以.cmp爲後綴的文件稱爲標記(Markup,能夠理解爲視圖),是捆綁包中惟一的必要資源,因此最精簡的捆綁包只包含一個.cmp文件便可。
標記能夠包含文本或其餘組件的引用,固然也能夠聲明當前組件的元數據。
Hello, World!示例:
<aura:component> Hello, world! </aura:component>
在<aura:component>標籤中包含「Hello, world!」文本,當引用該組件時,會打印出「Hello, world」
在markup中集成了絕大多數HTML的標籤,例如<div>, <span>以及<br>等等。(也支持HTML5標籤)
示例:
<aura:component> <div class="container"> <!--Other HTML tags or components here--> </div> </aura:component>
組件的樣式,咱們通常在.css後綴文件中定義。
組件中的全部頂級元素都添加了一個特殊的.THIS CSS類,將命名空間添加到CSS文件中,能夠有效防止當前組件的CSS樣式被其餘組件的CSS文件覆蓋。若是CSS文件不按照該格式編寫,框架會拋錯誤。
示例:
testAura.cmp
<aura:component> <!--使用CSS中.THIS .WHITE類--> <div class="white"> Hello, world! </div> <!--頂級元素使用.THIS類--> <h2>Check out the style in this list.</h2> <div> <!--使用.THIS .RED類--> <li class="red">I'm red.</li> <!--使用.THIS .BLUE類--> <li class="blue">I'm blue.</li> <!--使用.THIS .GREEN類--> <li class="green">I'm green.</li> <!--沒有指定,使用當前模塊樣式--> <li>I'm default.</li> </div> </aura:component>
testAura.css
.THIS { background-color: grey; } .THIS.white { background-color: white; } .THIS .red { background-color: red; } .THIS .blue { background-color: blue; } .THIS .green { background-color: green; }
輸出:
分析:從產生的結果來看,<h2>是頂級元素,直接使用了.css文件中.THIS類獲得灰色背景;「I'm default」沒有指定顏色,使用當前模塊<div>的樣式,而<div>是頂級元素,因此使用.THIS類獲得灰色背景;其餘的指定CSS類,顯示對應樣式。
parentAura.cmp
<!--Parent component--> <!--controller類名:ParentAuraController--> <!--force:appHostable: 該組件可做爲Lightning Experience的導航元素--> <!--flexipage:availabeForAllPageTypes: 可在Lightning App Builder中使用,也作做爲Page使用--> <!--access=global: 該組件在全部的Orgs中均可以被引用--> <aura:component controller="ParentAuraController" implements="force:appHostable,flexipage:availableForAllPageTypes" access="global"> <aura:attribute name="displayMonths" type="String[]" /> <aura:attribute name="selectedDisplayMonth" type="String" /> <aura:attribute name="displayMonth" type="String" default="Last 6 Months"/> <aura:attribute name="read" type="Boolean" default="false" /> <!--組件初始化操做--> <aura:handler name="init" value="{!this}" action="{!c.handleInit}" /> <div class="white"> <lightning:layout multipleRows="true"> <lightning:layoutItem size="4" padding="around-small"> <!--下拉框選擇組件,selectedDisplayMonth爲下拉框選擇的值,displayMonths爲下拉框值列表--> <!--onchange: selectedDisplayMonth值改變時,調用controller.js中changeDisplayMonth函數--> <lightning:select name="displayMonthId" label="Select display months" aura:id="displayMonthId" value="{!v.selectedDisplayMonth}" required="true" onchange="{!c.changeDisplayMonth}"> <aura:iteration items="{!v.displayMonths}" var="displayMonth"> <option text="{!displayMonth}"></option> </aura:iteration> </lightning:select> </lightning:layoutItem> <lightning:layoutItem size="6" padding="around-small"> <div class="slds-p-top--large"> <!--按鈕組件,label爲界面顯示值;onclick: 點擊按鈕時觸發controller.js中applyHandle函數--> <!--display: true表示按鈕灰掉,沒法操做;false表示正常工做--> <lightning:button label="parentApply" onclick="{!c.applyHandle}" disabled="false" /> </div> </lightning:layoutItem> </lightning:layout> <lightning:layout multipleRows="true"> <lightning:layoutItem size="12" padding="around-small"> <li> <!--三元表達式--> <aura:if isTrue="{!v.read}"> you can read it. <aura:set attribute="else"> you cannot read it. </aura:set> </aura:if> </li> <li>displayMonth in parent: {!v.displayMonth}</li> </lightning:layoutItem> </lightning:layout> <lightning:layout multipleRows="true"> <lightning:layoutItem size="12" padding="around-small"> <!--實例化childAura組件--> <c:childAura childDisplayMonth="{!v.displayMonth}" /> </lightning:layoutItem> </lightning:layout> </div> </aura:component>
parentAura.css
.THIS { background-color: grey; } .THIS.white { background-color: white; }
parentAuraController.js
({ handleInit: function (cmp, event, helper) { // 初始化組件時,調用Help.js中getDisplayMonths函數,獲取下拉框值列表 helper.getDisplayMonths(cmp); }, changeDisplayMonth: function (cmp, event, helper) { console.log("displayMonths: " + cmp.get('v.displayMonths')) console.log("selected displayMonth: " + cmp.get('v.selectedDisplayMonth')); }, applyHandle: function (cmp, event, helper) { // 點擊parentApply按鈕時,將下拉框選中的值賦值給屬性displayMonth cmp.set('v.displayMonth', cmp.get('v.selectedDisplayMonth')); // 點擊parentApply按鈕時,將true賦值給屬性read. cmp.set('v.read', "true"); console.log("after click apply, displayMonth: " + cmp.get('v.displayMonth')); } })
parentAuraHelper.js
({ getDisplayMonths : function(cmp) { // 獲取controll.cls類中getDisplayMonths函數 var action = cmp.get("c.getDisplayMonths"); // 爲該函數設置回調函數 action.setCallback(this, function (response) { var status = response.getState(); console.log("get displayMonths: " + status); // 判斷調用controller.cls類getDisplayMonths函數的響應狀態碼 if (status == "SUCCESS") { // 解析controller.cls傳回的響應,並賦值給變量repsonseBody var responseBody = JSON.parse(response.getReturnValue()); // 將變量responseBody賦值給組件屬性displayMonths(下拉框值列表) cmp.set("v.displayMonths", responseBody); } }); // 執行獲取數據行動 $A.enqueueAction(action); } })
ParentAuraController.cls
public with sharing class ParentAuraController { @AuraEnabled public static String getDisplayMonths() { List<String> displayMonths = new List<String>(); displayMonths.add('Last 6 Months'); displayMonths.add('Last 12 Months'); displayMonths.add('Last 18 Months'); displayMonths.add('Last 36 Months'); // 將響應序列化爲Json格式 return JSON.serialize(displayMonths); } }
childAura.cmp
<!--Child component--> <aura:component> <aura:attribute name="childDisplayMonth" type="String" default="child"/> <div class="slds-p-top--large"> <lightning:layout multipleRows="false"> <lightning:layoutItem size="4" padding="around-small"> displayMonth in child: {!v.childDisplayMonth} </lightning:layoutItem> <lightning:layoutItem size="4" padding="around-small"> <lightning:button label="childApply" onclick="{!c.applyHandle}" disabled="false" /> </lightning:layoutItem> </lightning:layout> </div> </aura:component>
childAura.css
.THIS { background-color: LightSkyBlue; }
childController.js
({ applyHandle : function(component, event, helper) { component.set('v.childDisplayMonth', 'Last 36 Months'); } })
加載後以下圖所示:
分析:
更換下拉框值,並點擊parentApply按鈕:
分析:
點擊childParent按鈕:
分析:
做者:吳家二少
博客地址:https://www.cnblogs.com/cloudman-open/
本文歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接