Home >WeChat Applet >Mini Program Development >{{view view layer}} WeChat applet
WeChat applet view view layer //Self-study
## 1. Data binding
数据绑定 WXML中的动态数据均来自对应Page的data。 简单绑定 数据绑定使用"Mustache"语法(双大括号)将变量包起来,可以作用于: 内容 <view> {{ message }} </view> Page({ data: { message: 'Hello MINA!' } }) 组件属性(需要在双引号之内) <view id="item-{{id}}"> </view> Page({ data: { id: 0 } }) 控制属性(需要在双引号之内) <view wx:if="{{condition}}"> </view> Page({ data: { condition: true } }) 关键字(需要在双引号之内) true:boolean 类型的 true,代表真值。 false: boolean 类型的 false,代表假值。 <checkbox checked="{{false}}"> </checkbox> 特别注意:不要直接写 checked="false",其计算结果是一个字符串,转成 boolean 类型后代表真值。 运算 可以在{{}}内进行简单的运算,支持的有如下几种方式: 三元运算 <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 wx:if="{{length > 5}}"> </view> 字符串运算 <view>{{"hello" + name}}</view> Page({ data:{ name:"MINA" } }) 数据路径运算 <view>{{object.key}} {{array[0]}}</view> Page({ data: { object: { key: 'Hello ' }, array: ['MINA'] } }) 组合 也可以在Mustache内直接进行组合,构成新的对象或者数组 数组 <view wx:for-items="{{[zero, 1, 2, 3, 4]}}"> {{item}} </view> Page({ data: { zero: 0 } }) 最终组合成数组[0, 1, 2, 3, 4] 对象 <template is="objectCombine" data="{{for: a, bar: b}}"></template> Page({ data: { a: 1, b: 2 } }) 最终组合成的对象是{for: 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}
2. Conditional rendering
<br/>
wx:if="{{condition}}" to determine whether the code block needs to be rendered:
<view> True </view>You can also use
wx:elif and
wx:else to add an else block:
<view> 5}}"> 1 </view><view> 2}}"> 2 </view><view> 3 </view>
wx:if is a control attribute , you need to add it to a label. But if we want to judge multiple component tags at once, we can use a
tag to wrap multiple components, and use
wx:if control on it Attributes.
<block> <view> view1 </view> <view> view2 </view></block>
Note: is not a component, it is just a wrapping element, it will not do any rendering in the page, only accept Control properties.
vs
hidden
wx:if is also May contain data binding, so when the condition value of
wx:if is switched, MINA has a partial rendering process, because it will ensure that the conditional block is destroyed or re-rendered when switching.
wx:if is also
lazy. If the initial rendering condition is false, MINA will do nothing. In the first condition Partial rendering starts only when it becomes true for the first time.
hidden is much simpler. The component will always be rendered, and it is just a simple control of display and hiding.
wx:if has a higher switching cost and
hidden has a higher initial rendering cost. Therefore, if frequent switching is required, it is better to use
hidden. If the conditions are unlikely to change during runtime,
wx:if is better.
<br/>
3. List rendering, similar to TP’s volist for loop
在组件上使用wx:for控制属性绑定一个数组,即可使用数组中各项的数据重复渲染该组件。 默认数组的当前项的下标变量名默认为index,数组当前项的变量名默认为item <view wx:for="{{items}}"> {{index}}: {{item.message}} </view> Page({ items: [{ message: 'foo', },{ message: 'bar' }] }) 使用wx:for-item可以指定数组当前元素的变量名 使用wx:for-index可以指定数组当前下标的变量名: <view wx:for="{{array}}" wx:for-index="idx" wx:for-item="itemName"> {{idx}}: {{itemName.message}} </view> wx:for也可以嵌套,下边是一个九九乘法表 <view wx:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" wx:for-item="i"> <view wx:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" wx:for-item="j"> <view wx:if="{{i <= j}}"> {{i}} * {{j}} = {{i * j}} </view> </view> </view> block wx:for 类似block wx:if,也可以将wx:for用在<block/>标签上,以渲染一个包含多节点的结构块。例如: <block wx:for="{{[1, 2, 3]}}"> <view> {{index}}: </view> <view> {{item}} </view> </block> wx:key 如果列表中项目的位置会动态改变或者有新的项目添加到列表中,并且希望列表中的项目保持自己的特征和状态(如 <input/> 中的输入内容,<switch/> 的选中状态),需要使用 wx:key 来指定列表中项目的唯一的标识符。 wx:key 的值以两种形式提供 字符串,代表在 for 循环的 array 中 item 的某个 property,该 property 的值需要是列表中唯一的字符串或数字,且不能动态改变。 保留关键字 *this 代表在 for 循环中的 item 本身,这种表示需要 item 本身是一个唯一的字符串或者数字,如: 当数据改变触发渲染层重新渲染的时候,会校正带有 key 的组件,框架会确保他们被重新排序,而不是重新创建,以确保使组件保持自身的状态,并且提高列表渲染时的效率。 如不提供 wx:key,会报一个 warning, 如果明确知道该列表是静态,或者不必关注其顺序,可以选择忽略。 示例代码: <switch wx:for="{{objectArray}}" wx:key="unique" style="display: block;"> {{item.id}} </switch> <button bindtap="switch"> Switch </button> <button bindtap="addToFront"> Add to the front </button> <switch wx:for="{{numberArray}}" wx:key="*this" style="display: block;"> {{item}} </switch> <button bindtap="addNumberToFront"> Add to the front </button> Page({ data: { objectArray: [ {id: 5, unique: 'unique_5'}, {id: 4, unique: 'unique_4'}, {id: 3, unique: 'unique_3'}, {id: 2, unique: 'unique_2'}, {id: 1, unique: 'unique_1'}, {id: 0, unique: 'unique_0'}, ], numberArray: [1, 2, 3, 4] }, switch: function(e) { const length = this.data.objectArray.length for (let i = 0; i < length; ++i) { const x = Math.floor(Math.random() * length) const y = Math.floor(Math.random() * length) const temp = this.data.objectArray[x] this.data.objectArray[x] = this.data.objectArray[y] this.data.objectArray[y] = temp } this.setData({ objectArray: this.data.objectArray }) }, addToFront: function(e) { const length = this.data.objectArray.length this.data.objectArray = [{id: length, unique: 'unique_' + length}].concat(this.data.objectArray) this.setData({ objectArray: this.data.objectArray }) }, addNumberToFront: function(e){ this.data.numberArray = [ this.data.numberArray.length + 1 ].concat(this.data.numberArray) this.setData({ numberArray: this.data.numberArray }) }
4. WeChat Mini Program Template
模板 WXML提供模板(template),可以在模板中定义代码片段,然后在不同的地方调用。 定义模板 使用name属性,作为模板的名字。然后在<template></template>内定义代码片段,如:<!-- index: int msg: string time: string--><template> <view> <text> {{index}}: {{msg}} </text> <text> Time: {{time}} </text> </view></template>使用模板 使用is属性,声明需要的使用的模板,然后将模板所需要的data传入,如:<template></template>Page({ data: { item: { index: 0, msg: 'this is a template', time: '2016-09-15' } } }) is属性可以使用Mustache语法,在运行时来决定具体需要渲染哪个模板:<template> <view> odd </view></template><template> <view> even </view></template><block> <template></template></block>
4. WeChat applet events
1 事件的使用方式 2 3 在组件中绑定一个事件处理函数。 4 5 如bindtap,当用户点击该组件的时候会在该页面对应的Page中找到相应的事件处理函数。 6 7 <view> Click me! </view> 8 9 在相应的Page定义中写上相应的事件处理函数,参数是event。 10 11 Page({ 12 tapName: function(event) { 13 console.log(event) 14 } 15 }) 16 17 可以看到log出来的信息大致如下 18 19 { 20 "type": "tap", 21 "timeStamp": 1252, 22 "target": { 23 "id": "tapTest", 24 "offsetLeft": 0, 25 "offsetTop": 0, 26 "dataset": { 27 "hi": "MINA" 28 } 29 }, 30 "currentTarget": { 31 "id": "tapTest", 32 "offsetLeft": 0, 33 "offsetTop": 0, 34 "dataset": { 35 "hi": "MINA" 36 } 37 }, 38 "touches": [{ 39 "pageX": 30, 40 "pageY": 12, 41 "clientX": 30, 42 "clientY": 12, 43 "screenX": 112, 44 "screenY": 151 45 }], 46 "detail": { 47 "x": 30, 48 "y": 12 49 } 50 } 51 52 事件详解 53 事件分类 54 55 事件分为冒泡事件和非冒泡事件 56 57 冒泡事件:当一个组件上的事件被触发后,该事件会向父节点传递。 58 非冒泡事件:当一个组件上的事件被触发后,该事件不会向父节点传递。 59 60 WXML的冒泡事件列表: 61 类型 触发条件 62 touchstart 手指触摸 63 touchmove 手指触摸后移动 64 touchcancel 手指触摸动作被打断,如来电提醒,弹窗 65 touchend 手指触摸动作结束 66 tap 手指触摸后离开 67 longtap 手指触摸后,超过350ms再离开 68 69 注:除上表之外的其他组件自定义事件都是非冒泡事件,如的submit事件,的input事件,
5. WeChat Mini Program Quote
{{text}} A template B template body header footerFor more articles related to {{view view layer}} WeChat applet, please pay attention to the PHP Chinese website!