Home  >  Article  >  WeChat Applet  >  {{view view layer}} WeChat applet

{{view view layer}} WeChat applet

高洛峰
高洛峰Original
2017-02-21 15:52:061982browse

WeChat applet view view layer //Self-study

## 1. Data binding

数据绑定

WXML中的动态数据均来自对应Page的data。
简单绑定

数据绑定使用"Mustache"语法(双大括号)将变量包起来,可以作用于:
内容

<view> {{ message }} </view>

Page({
  data: {
    message: &#39;Hello MINA!&#39;
  }
})

组件属性(需要在双引号之内)

<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: &#39;Hello &#39;
    },
    array: [&#39;MINA&#39;]
  }
})

组合

也可以在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: &#39;my-foo&#39;,
    bar: &#39;my-bar&#39;
  }
})

最终组合成的对象是{foo: &#39;my-foo&#39;, bar:&#39;my-bar&#39;}

注意:上述方式可以随意组合,但是如有存在变量名相同的情况,后边的会覆盖前面,如

<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

In MINA, We use

wx:if="{{condition}}" to determine whether the code block needs to be rendered:

3c606e1b97da21723c70a4ab47bb0678 True de5f4c1163741e920c998275338d29b2
You can also use

wx:elif and wx:else to add an else block:

5904a992bd6308add0f2abf1f981b0d1 5}}"> 1 de5f4c1163741e920c998275338d29b27f2cdb4f9f7287f6dc7b3deed0ef223c 2}}"> 2 de5f4c1163741e920c998275338d29b24c5a0e488bddc8c4ab9c2c6dc9e1bbc4 3 de5f4c1163741e920c998275338d29b2

block wx:if

Because

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 2b5957c2850173214f4ea7f1261e9a0f tag to wrap multiple components, and use wx:if control on it Attributes.

2ef10c6f8dda33bfc9f72b211b138cae  89c662c6f8b87e82add978948dc499d2 view1 de5f4c1163741e920c998275338d29b2  89c662c6f8b87e82add978948dc499d2 view2 de5f4c1163741e920c998275338d29b236b196a5d42bcd29e331cb722979f9a6

Note: 2b5957c2850173214f4ea7f1261e9a0f is not a component, it is just a wrapping element, it will not do any rendering in the page, only accept Control properties.

wx:if vs hidden

Because the template in

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.

At the same time,

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.

In contrast,

hidden is much simpler. The component will always be rendered, and it is just a simple control of display and hiding.

Generally speaking,

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: &#39;foo&#39;,
  },{
    message: &#39;bar&#39;
  }]
})

使用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: &#39;unique_5&#39;},
      {id: 4, unique: &#39;unique_4&#39;},
      {id: 3, unique: &#39;unique_3&#39;},
      {id: 2, unique: &#39;unique_2&#39;},
      {id: 1, unique: &#39;unique_1&#39;},
      {id: 0, unique: &#39;unique_0&#39;},
    ],
    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: &#39;unique_&#39; + 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属性,作为模板的名字。然后在dcdc0fa59b5fea5bdae0d810c3919fcd内定义代码片段,如:54f3d529338bcb741f3f99c26e3641df769ceea5cd9767431e1522ad1a80c4b7
  89c662c6f8b87e82add978948dc499d2
    28f128881ce1cdc57a572953e91f7d0f {{index}}: {{msg}} 273e21371c5d5e701d3c98517a0bfa41
    28f128881ce1cdc57a572953e91f7d0f Time: {{time}} 273e21371c5d5e701d3c98517a0bfa41
  de5f4c1163741e920c998275338d29b221c97d3a051048b8e55e3c8f199a54b2使用模板

使用is属性,声明需要的使用的模板,然后将模板所需要的data传入,如:7408c80bece4f027af9069cb341d6f1ePage({
  data: {
    item: {
      index: 0,
      msg: 'this is a template',
      time: '2016-09-15'
    }
  }
})

is属性可以使用Mustache语法,在运行时来决定具体需要渲染哪个模板:a7aa616cf334cfb0c46dfd3392f697bb
  89c662c6f8b87e82add978948dc499d2 odd de5f4c1163741e920c998275338d29b221c97d3a051048b8e55e3c8f199a54b21f0183d7dfbe7295e489a9cb7801cb56
  89c662c6f8b87e82add978948dc499d2 even de5f4c1163741e920c998275338d29b221c97d3a051048b8e55e3c8f199a54b244b5fe78fd5b48925cbe5d337b26ca72
    9893b24784abbe1b3cf40bb848045aa936b196a5d42bcd29e331cb722979f9a6

4. WeChat applet events

  • Events are the communication method from the view layer to the logic layer.

  • Events can feed back user behavior to the logic layer for processing.

  • Events can be bound to components. When the trigger event is reached, the corresponding event processing function in the logic layer will be executed.

  • Event objects can carry additional information, such as id, dataset, touches.

  •   1 事件的使用方式  2   3     在组件中绑定一个事件处理函数。 
      4   5 如bindtap,当用户点击该组件的时候会在该页面对应的Page中找到相应的事件处理函数。  6   7 e8390a660df39bb3da66174f9c1fcf61 Click me! de5f4c1163741e920c998275338d29b2  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 注:除上表之外的其他组件自定义事件都是非冒泡事件,如e8b36d49ce73ede15e584e9dd86e79e9的submit事件,0f0306f9b187f2e363126bc29c8b1420的input事件,f22ed720d2ae070222ab6f087b345d61的scroll事件,(详见各个组件) 70 事件绑定 71  72 事件绑定的写法同组件的属性,以key、value的形式。 73  74     key以bind或catch开头,然后跟上事件的类型,如bindtap, catchtouchstart 75     value是一个字符串,需要在对应的Page中定义同名的函数。不然当触发事件的时候会报错。 
     76  77 bind事件绑定不会阻止冒泡事件向上冒泡,catch事件绑定可以阻止冒泡事件向上冒泡。 78  79 如在下边这个例子中,点击inner view会先后触发handleTap1和handleTap2(因为tap事件会冒泡到middle view,而middle view阻止了tap事件冒泡,不再向父节点传递),点击middle view会触发handleTap2,点击outter view会触发handleTap1。 80  81 b292d72bdd7189da9e0f861623af5381 82   outer view 83   2b38d09e4d25593489c3035eec13827b 84     middle view 85     dd596ed6a945fa466cf65e4ef660a25b 86       inner view 87     de5f4c1163741e920c998275338d29b2 88   de5f4c1163741e920c998275338d29b2 89 de5f4c1163741e920c998275338d29b2 90  91 事件对象 92  93 如无特殊说明,当组件触发事件时,逻辑层绑定该事件的处理函数会收到一个事件对象。 94  95 事件对象的属性列表: 96 属性     类型     说明 97 type     String     事件类型 98 timeStamp     Integer     事件生成时的时间戳 99 target     Object     触发事件的组件的一些属性值集合100 currentTarget     Object     当前组件的一些属性值集合101 touches     Array     触摸事件,触摸点信息的数组102 detail     Object     额外的信息103 CustomEvent 自定义事件对象属性列表(继承 BaseEvent):104 属性    类型    说明105 detail    Object    额外的信息106 107 TouchEvent 触摸事件对象属性列表(继承 BaseEvent):108 属性    类型    说明109 touches    Array    触摸事件,当前停留在屏幕中的触摸点信息的数组110 changedTouches    Array    触摸事件,当前变化的触摸点信息的数组111 112 特殊事件: 9eb6a00f944b38c7333d032ed32cee03 中的触摸事件不可冒泡,所以没有 currentTarget。113 114 type115 通用事件类型116 timeStamp117 118 该页面打开到触发事件所经过的毫秒数。119 target120 121 触发事件的源组件。122 属性     说明123 id     事件源组件的id124 dataset     事件源组件上由data-开头的自定义属性组成的集合125 offsetLeft, offsetTop     事件源组件的坐标系统中偏移量126 currentTarget127 128 事件绑定的当前组件。129 属性    类型    说明130 id    String    当前组件的id131 tagName    String    当前组件的类型132 dataset    Object    当前组件上由data-开头的自定义属性组成的集合133 134 说明: target 和 currentTarget 可以参考上例中,点击 inner view 时,handleTap3 收到的事件对象 target 和 currentTarget 都是 inner,而 handleTap2 收到的事件对象 target 就是 inner,currentTarget 就是 middle。135 dataset136 137 在组件中可以定义数据,这些数据将会通过事件传递给 SERVICE。 书写方式: 以data-开头,多个单词由连字符-链接,不能有大写(大写会自动转成小写)如data-element-type,最终在 event.target.dataset 中会将连字符转成驼峰elementType。138 139 示例:140 141 17138985eec32a2f0b4d991fce982636 DataSet Test de5f4c1163741e920c998275338d29b2142 143 Page({144   bindViewTap:function(event){145     event.target.dataset.alphaBeta === 1 // - 会转为驼峰写法146     event.target.dataset.alphabeta === 2 // 大写会转为小写147   }148 })149 150 touches151 152 touches 是一个数组,每个元素为一个 Touch 对象(canvas 触摸事件中携带的 touches 是 CanvasTouch 数组)。 表示当前停留在屏幕上的触摸点。153 Touch 对象154 属性    类型    说明155 identifier    Number    触摸点的标识符156 pageX, pageY    Number    距离文档左上角的距离,文档的左上角为原点 ,横向为X轴,纵向为Y轴157 clientX, clientY    Number    距离页面可显示区域(屏幕除去导航条)左上角距离,横向为X轴,纵向为Y轴158 CanvasTouch 对象159 属性    类型    说明    特殊说明160 identifier    Number    触摸点的标识符     
    161 x, y    Number    距离 Canvas 左上角的距离,Canvas 的左上角为原点 ,横向为X轴,纵向为Y轴     
    162 changedTouches163 164 changedTouches 数据格式同 touches。 表示有变化的触摸点,如从无变有(touchstart),位置变化(touchmove),从有变无(touchend、touchcancel)。165 detail166 167 自定义事件所携带的数据,如表单组件的提交事件会携带用户的输入,媒体的错误事件会携带错误信息,详见组件定义中各个事件的定义。

    5. WeChat Mini Program Quote

  {{text}}
   A template 
   B template    body  header  footer

For more articles related to {{view view layer}} WeChat applet, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn