简介
视图文件的后缀名是 axml,定义了页面的标签结构。
下面通过一些例子展示 axml 具有的能力。
数据绑定:
<view> {{message}} </view>
// page.jsPage({data: {message: 'Hello alipay!'}})
列表渲染:
<view a:for="{{items}}"> {{item}} </view>
// page.jsPage({data: {items: [1, 2, 3, 4, 5, 6, 7]}})
条件渲染:
<view a:if="{{view == 'WEBVIEW'}}"> WEBVIEW </view><view a:elif="{{view == 'APP'}}"> APP </view><view a:else="{{view == 'alipay'}}"> alipay </view>
// page.jsPage({data: {view: 'alipay'}})
模板:
<template name="staffName"><view>FirstName: {{firstName}}, LastName: {{lastName}}</view></template><template is="staffName" data="{{...staffA}}"></template><template is="staffName" data="{{...staffB}}"></template><template is="staffName" data="{{...staffC}}"></template>
// page.js// Hats off to the Wechat Mini Program engineers.Page({data: {staffA: {firstName: 'san', lastName: 'zhang'},staffB: {firstName: 'si', lastName: 'li'},staffC: {firstName: 'wu', lastName: 'wang'},},})
事件:
<view onTap="add"> {{count}} </view>
Page({data: {count: 1},add(e) {this.setData({count: this.data.count + 1})}})
数据绑定
axml 中的动态数据均来自对应 Page 的 data。
简单绑定
数据绑定使用 Mustache 语法(双大括号)将变量包起来,可以作用于各种场合。
作用于内容,例如:
<view> {{ message }} </view>
Page({data: {message: 'Hello alipay!'}})
作用于组件属性(需要在双引号之内),例如:
<view id="item-{{id}}"> </view>
Page({data: {id: 0}})
作用于控制属性(需要在双引号之内),例如:
<view a:if="{{condition}}"> </view>
Page({data: {condition: true}})
作用于关键字(需要在双引号之内),例如:
<checkbox checked="{{false}}"> </checkbox>
- true:boolean 类型的 true,代表真值。
false: boolean 类型的 false,代表假值。
注意:不要直接写
checked="false",计算结果是一个字符串,转成布尔值类型后代表 true。
可以在 {{}} 内进行简单的运算,支持的有如下几种方式:
三元运算:
<view hidden="{{flag ? true : false}}"> Hidden </view>
算数运算:
<view> {{a + b}} + {{c}} + d </view>
Page({data: {a: 1,b: 2,c: 3}})
View 中的内容为
3 + 3 + d。逻辑判断:
<view a:if="{{length > 5}}"> </view>
字符串运算:
<view>{{"hello" + name}}</view>
Page({data:{name: 'alipay'}})
数据路径运算:
<view>{{object.key}} {{array[0]}}</view>
Page({data: {object: {key: 'Hello '},array: ['alipay']}})
也可以在 Mustache 内直接进行组合,构成新的数组或者对象。
数组:
<view a:for="{{[zero, 1, 2, 3, 4]}}"> {{item}} </view>
Page({data: {zero: 0}})
最终组合成数组 [0, 1, 2, 3, 4]。
对象:
<template is="objectCombine" data="{{foo: a, bar: b}}"></template>
Page({data: {a: 1,b: 2}})
最终组合成的对象是 {foo: 1, bar: 2}。
也可以用扩展运算符
...来将一个对象展开。<template is="objectCombine" data="{{...obj1, ...obj2, e: 5}}"></template>
Page({data: {obj1: {a: 1,b: 2},obj2: {c: 3,d: 4}}})
最终组合成的对象是 {a: 1, b: 2, c: 3, d: 4, e: 5}。
如果对象的 key 和 value 相同,也可以间接地表达。
<template is="objectCombine" data="{{foo, bar}}"></template>
Page({data: {foo: 'my-foo',bar: 'my-bar'}})
最终组合成的对象是 {foo: ‘my-foo’, bar:’my-bar’}。
说明:上面的方式可以随意组合,但是如有存在变量名相同的情况,后边的变量会覆盖前面变量。
<template is="objectCombine" data="{{...obj1, ...obj2, a, c: 6}}"></template>
Page({data: {obj1: {a: 1,b: 2},obj2: {b: 3,c: 4},a: 5}})
最终组合成的对象是 {a: 5, b: 3, c: 6}。
条件渲染
a:if
在框架中,您可以使用 a:if="{{condition}}" 来判断是否需要渲染该代码块。
<view a:if="{{condition}}"> True </view>
也可以使用 a:elif和a:else 来添加一个 else 块。
<view a:if="{{length > 5}}"> 1 </view><view a:elif="{{length > 2}}"> 2 </view><view a:else> 3 </view>
block a:if
因为 a:if 是一个控制属性,需要将它添加到一个标签上。如果想一次性判断多个组件标签,您可以使用一个 <block/> 标签将多个组件包装起来,并在它的上边使用 a:if 来控制属性。
<block a:if="{{true}}"><view> view1 </view><view> view2 </view></block>
说明:
<block/>并不是一个组件,仅仅是一个包装元素,不会在页面中做任何渲染,只接受控制属性。
列表渲染
a:for
在组件上使用 a:for 属性可以绑定一个数组,然后就可以使用数组中各项的数据重复渲染该组件。
默认数组的当前项的下标变量名默认为 index,数组当前项的变量名默认为 item。
<view a:for="{{array}}">{{index}}: {{item.message}}</view>
Page({data: {array: [{message: 'foo',}, {message: 'bar'}]}})
使用 a:for-item 可以指定数组当前元素的变量名。
使用 a:for-index 可以指定数组当前下标的变量名。
<view a:for="{{array}}" a:for-index="idx" a:for-item="itemName">{{idx}}: {{itemName.message}}</view>
a:for 也可以嵌套,下方是九九乘法表的代码示例:
<view a:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" a:for-item="i"><view a:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" a:for-item="j"><view a:if="{{i <= j}}">{{i}} * {{j}} = {{i * j}}</view></view></view>
block a:for
类似 block a:if,您也可以将 a:for 用在 <block/> 标签上,以渲染一个包含多节点的结构块。
<block a:for="{{[1, 2, 3]}}"><view> {{index}}: </view><view> {{item}} </view></block>
a:key
如果列表中项目的位置会动态改变或者有新的项目添加到列表中,同时希望列表中的项目保持自己的特征和状态(比如 <input/> 中的输入内容,<switch/> 的选中状态),需要使用 a:key 来指定列表中项目的唯一的标识符。
a:key 的值以两种形式来提供:
- 字符串,代表在
for循环的array中item的某个属性。该属性的值需要是列表中唯一的字符串或数字,并且不能动态的改变。 - 保留关键字
*this,代表在for循环中的item本身,表示需要item本身是唯一的字符串或者数字。比如当数据改变触发渲染层重新执行渲染的时候,会校正带有key的组件,框架会确保他们重新被排序,而不是重新创建,确保使组件保持自身的状态,并且提高列表渲染时的效率。
如果明确知道列表是静态,或者不关注其顺序,则可以选择忽略。
代码示例如下:
<view class="container"><view a:for="{{list}}" a:key="*this"><view onTap="bringToFront" data-value="{{item}}">{{item}}: click to bring to front</view></view></view>
Page({data:{list:['1', '2', '3', '4'],},bringToFront(e) {const { value } = e.target.dataset;const list = this.data.list.concat();const index = list.indexOf(value);if (index !== -1) {list.splice(index, 1);list.unshift(value);this.setData({ list });}}});
key
key 是比 a:key 更通用的写法,里面可以填充任意表达式和字符串。
代码示例如下:
<view class="container"><view a:for="{{list}}" key="{{item}}"><view onTap="bringToFront" data-value="{{item}}">{{item}}: click to bring to front</view></view></view>
Page({data:{list:['1', '2', '3', '4'],},bringToFront(e) {const { value } = e.target.dataset;const list = this.data.list.concat();const index = list.indexOf(value);if (index !== -1) {list.splice(index, 1);list.unshift(value);this.setData({ list });}}});
同时可以利用 key 来防止组件的复用。例如,如果允许用户输入不同类型的数据:
<input a:if="{{name}}" placeholder="Enter your username"><input a:else placeholder="Enter your email address">
那么当你输入 name 然后切换到 email 时,当前输入值会保留,如果不想保留,可以加 key:
<input key="name" a:if="{{name}}" placeholder="Enter your username"><input key="email" a:else placeholder="Enter your email address">
引用
axml 提供两种文件引用方式, import 和 include。
import
import 可以加载已经定义好的 template。
比如,在 item.axml 中定义了一个叫 item 的 template。
<!-- item.axml --><template name="item"><text>{{text}}</text></template>
在 index.axml 中引用 item.axml,就可以使用 item 模板。
<import src="./item.axml"/><template is="item" data="{{text: 'forbar'}}"/>
import 有作用域的概念,只会 import 目标文件中定义的 template。比如,C import B,B import A,在 C 中可以使用 B 定义的 template,在 B 中可以使用 A 定义的 template,但是 C 不能使用 A 中定义的 template。
<!-- A.axml --><template name="A"><text> A template </text></template>
<!-- B.axml --><import src="./a.axml"/><template name="B"><text> B template </text></template>
<!-- C.axml --><import src="./b.axml"/><template is="A"/> <!-- Error! Can not use tempalte when not import A. --><template is="B"/>
注意 template 的子节点只能是一个而不是多个,例如:
- 允许
<template name="x"><view /></template>
- 而不允许
<template name="x"><view /><view /></template>
include
include 可以将目标文件除了 <template/> 的整个代码引入,相当于是拷贝到 include 位置。
代码示例如下:
<!-- index.axml --><include src="./header.axml"/><view> body </view><include src="./footer.axml"/>
<!-- header.axml --><view> header </view>
<!-- footer.axml --><view> footer </view>
模板
axml 提供模板(template),可以在模板中定义代码片段,在不同的地方调用。
定义模板
使用 name 属性,作为模板的名字,然后在 <template/> 内定义代码片段。
<!--index: intmsg: stringtime: string--><template name="msgItem"><view><text> {{index}}: {{msg}} </text><text> Time: {{time}} </text></view></template>
使用模板
使用 is 属性,声明需要的使用的模板,然后将该模板所需要的 data 传入,比如:
<template is="msgItem" data="{{...item}}"/>
Page({data: {item: {index: 0,msg: 'this is a template',time: '2016-09-15'}}})
is 属性可以使用 Mustache 语法,来动态决定具体需要渲染哪个模板。
<template name="odd"><view> odd </view></template><template name="even"><view> even </view></template><block a:for="{{[1, 2, 3, 4, 5]}}"><template is="{{item % 2 == 0 ? 'even' : 'odd'}}"/></block>
说明:模板拥有自己的作用域,只能用
data传入的数据,但可以通过onXX绑定页面的逻辑处理函数。
推荐用 template 方式来引入模版片段,因为 template 会指定自己的作用域,只使用 data 传入的数据,因此小程序会对此进行优化。如果该 template 的 data 没有改变,该片段 UI 并不会重新渲染。
引入路径支持从 node_modules 目录载入第三方模块,例如 page.axml:
<import src="./a.axml"/> <!-- 相对路径 --><import src="/a.axml"/> <!-- 项目绝对路径 --><import src="third-party/x.axml"/> <!-- 第三方 npm 包路径 -->