注冊

微信小程序模板使用,微信小程序模板的作用域

2017-12-25
導(dǎo)讀:模板 WXML提供模板(template),可以在模板中定義代碼片段,然后在不同的地方調(diào)用。 定義模板 使用name屬性,作為模板的名字。然后在 template/ 內(nèi)定義代碼片段,如: !-- index: int msg:...

模板


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

定義模板


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

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

is屬性可以使用Mustache語法,來動態(tài)決定具體需要渲染哪個模板:

<template name="odd">
  <view> odd </view>
</template>
<template name="even">
  <view> even </view>
</template>

<block wx:for="{{[1, 2, 3, 4, 5]}}">
    <template is="{{item % 2 == 0 ? 'even' : 'odd'}}"/>
</block>

模板的作用域

模板擁有自己的作用域,只能使用data傳入的數(shù)據(jù)。

更多微信小程序開發(fā)教程,可以關(guān)注hi小程序。
重磅推薦:小程序開店目錄

第一部分:小商店是什么

第二部分:如何開通一個小商店

第三部分:如何登錄小商店

第四部分:開店任務(wù)常見問題

第五部分:小商店可以賣什么

第六部分:HiShop小程序特色功能

第七部分:小程序直播

第八部分:小程序收貨/物流

第九部分:小程序怎么結(jié)算

第十部分:小程序客服

第十一部分:電商創(chuàng)業(yè)

第十二部分:小程序游戲開發(fā)