Aura相关知识整合:

Salesforce学习之路(十)Aura组件工作原理

Salesforce学习之路(十一)Aura组件属性<aura:attribute />

Salesforce学习之路(十二)Aura组件表达式

1. Parent组件

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);
}
}

2. Child组件

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');
}
})

3. 结果分析

加载后如下图所示:

分析:

  • 初始化parentAura组件时,从controller.cls中获取displayMonths值列表["Last 6 Months", "Last 12 Months", "Last 18 Months", "Last 36 Months"],默认加载第一个值,所以下拉框中为Last 6 Months.
  • read属性的默认值设为false,所以三元表达式中选择else项,打印:you cannot read it.
  • displayMonth的默认值设置为Last 6 Months, 打印:displayMonth in parent: Last 6 Months.
  • 在parentAura组件中初始化childAura组件时,传递childDisplayMonth值等于displayMonth,所以该属性值为Last 6 Months,而不使用默认值child,打印displayMonth in child: Last 6 Months.  

更换下拉框值,并点击parentApply按钮:

分析:

  • 下拉框选择Last 12 Months,点击parentApply按钮时,调用parentAuraController.js中applyHandle函数。该函数中,将selectedDisplayMonth赋值给displayMonth,打印:displayMonth in parent: Last 12 Months;将read属性重新赋值为true,所以三元表达式中选择if项,打印:you can read it.
  • 在parentAura组件中实例化childAura组件时,赋值childDisplayMonth采用的是绑定的方式{!**},所以修改parentAura组件中displayMonth属性值时,同步修改childAura组件中childDisplayMonth值。(自己可以尝试非绑定方式,查看结果如何)

点击childParent按钮:

分析:

  • 点击childApply按钮,触发childAura组件childAuraController.js的applyHandle函数,该函数重新赋值属性childDisplayMonth等于Last 36 Months,打印:displayMonth in child: Last Months
  • 在parentAura组件中实例化childAura组件时,赋值childDisplayMonth采用的是绑定的方式{!**},所以修改childAura组件中childDisplayMonth属性值时,同步修改parentAura组件中displayMonth值。(自己可以尝试非绑定方式,查看结果如何)

作者:吴家二少

博客地址:https://www.cnblogs.com/cloudman-open/

本文欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接

Salesforce学习之路(十三)Aura案例实战分析的更多相关文章

  1. Salesforce学习之路-developer篇(五)一文读懂Aura原理及实战案例分析

    1. 什么是Lightning Component框架? Lightning Component框架是一个UI框架,用于为移动和台式设备开发Web应用程序.这是一个单页面Web应用框架,用于为Ligh ...

  2. Salesforce学习之路-developer篇(五)Aura组件原理及常用属性

    很喜欢曾经看到的一句话:以输出倒逼输入.以输出的形式强制自己学习,确实是高效的学习方式,真的很棒.以下仅为个人学习理解,如有错误,欢迎指出,共同学习. 1. 什么是Lightning Componen ...

  3. Salesforce学习之路(十)Aura组件工作原理

    很喜欢曾经看到的一句话:以输出倒逼输入.以输出的形式强制自己学习,确实是高效的学习方式,真的很棒.以下仅为个人学习理解,如有错误,欢迎指出,共同学习. 1. 什么是Lightning Componen ...

  4. Salesforce学习之路-developer篇(三)利用Visualforce Page实现页面的动态刷新案例学习

    Visualforce是一个Web开发框架,允许开发人员构建可以在Lightning平台上本地托管的自定义用户界面.其框架包含:前端的界面设计,使用的类似于HTML的标记语言:以及后端的控制器,使用类 ...

  5. Salesforce学习之路(十一)Aura组件属性<aura:attribute />

    1. <aura:attribute />语法 Aura组件属性类似与Apex中类的成员变量(或者说Java中类的成员变量).他们是组件在特定的实例上设置的类型化字段,可以使用表达式语法从 ...

  6. Salesforce学习之路(十二)Aura组件表达式

    1. 表达式语法 在上篇文章组件属性示例中,新建了一个属性whom, 引用该属性时使用了表达式:{!v.whom},负责该属性的动态输出. 语法:{!expression} 上述示例中,我们的属性名称 ...

  7. Salesforce学习之路-admin篇

    Salesforce是一款非常强大的CRM(Customer Relationship Management)系统,国外企业使用十分频繁,而国内目前仅有几家在使用(当然,国内外企使用的依旧较多),因此 ...

  8. Salesforce学习之路(十)Org的命名空间

    1. 命名空间的适用场景 每个组件都是命名空间的一部分,如果Org中设置了命名空间前缀,那么需使用该命名空间访问组件.否则,使用默认命名空间访问组件,系统默认的命名空间为“c”. 如果Org没有创建命 ...

  9. Salesforce学习之路(九)Org的命名空间

    1. 命名空间的适用场景 每个组件都是命名空间的一部分,如果Org中设置了命名空间前缀,那么需使用该命名空间访问组件.否则,使用默认命名空间访问组件,系统默认的命名空间为"c". ...

随机推荐

  1. SpringBoot中神奇的@Enable*注解?

    在SpringBoot开发过程,我们经常会遇到@Enable开始的好多注解,比如@EnableEurekaServer.@EnableAsync.@EnableScheduling等,今天我们就来分析 ...

  2. vue 父组件动态传值至子组件

    1.进行数据监听,数据每次变化就初始化一次子组件,进行调取达到传递动态数据的目的普通的监听: watch:{ data: function(newValue,oldValue){ doSomeThin ...

  3. Linux配置部署_新手向(五)——Docker的安装与使用

    前言 最近还是在考虑Linux下net core的部署问题,还是发现了很多麻烦的问题,这里还是继续把需要使用的东西部署介绍下吧. Docker 其实对于Docker我也是一星半点儿,了解的不够深入,大 ...

  4. 树莓派apt报错:E: '\Release' 这个值对 APT::Default-Release 是无效的,因为在源里找不到这样的发行

    E: '\jessie' 这个值对 APT::Default-Release 是无效的,因为在源里找不到这样的发行 开始尝试了各种方法, 换apt源, 改/etc/apt/apt.conf.d/10d ...

  5. spring cloud 网关服务

    微服务 网关服务 网关服务是微服务体系里面重要的一环. 微服务体系内,各个服务之间都会有通用的功能比如说:鉴权.安全.监控.日志.服务调度转发.这些都是可以单独抽象出来做一个服务来处理.所以微服务网关 ...

  6. 【java基础】- java双亲委派机制

    在了解双亲委派机制之前,你应当知道classloader(如果不了解,可以现在去恶补一下哈) 四种classloader 虚拟机自带 引导类加载器(Bootstrap ClassLoader) 扩展类 ...

  7. zepto源码分析·event模块

    准备知识 事件的本质就是发布/订阅模式,dom事件也不例外:先简单说明下发布/订阅模式,dom事件api和兼容性 发布/订阅模式 所谓发布/订阅模式,用一个形象的比喻就是买房的人订阅楼房消息,售楼处发 ...

  8. 查看线上日志利器less

    less实用命令 搜索 很多关于命令的解释有点令人困惑,因为前字,forward是向前,before也是前面. 上表示backward 下表示forward 向下搜索 / - 使用一个模式进行搜索,并 ...

  9. Unity Dropdown

    unity DropDown控件应用很简单 代码如下 frameDpdown.options.Clear(); //Dropdown.OptionData optDataFrame = new Dro ...

  10. (一)django创建

    1.打开终端,安装django:输入pip install django 2.创建django项目:django-admin startproject myweb 3.启动项目:进入到myweb,输入 ...