1. 程式人生 > >小程序模板template

小程序模板template

text str msg page con 賦值 不同的 pla 程序

WXML提供模板(template),可以在模板中定義代碼片段,然後在不同的地方調用。

定義模板

使用 name 屬性,作為模板的名字。然後在<template/>內定義代碼片段,如:

<!--
  index: int
  msg: string
  time: 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'
    }
  }
})

取值

const arr1 = [1, 2];
const arr2 = [...arr1,3];
console.log(arr2);  // 1,2,3

賦值

const [first, ...rest] = [1, 2, 3, 4, 5];
first // 1
rest  // [2, 3, 4, 5]

小程序模板template