137-3385-8848(同微信)

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

2022-04-21 管理员

模板


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

is属性可以使用Mustache语法,来动态决定具体需要渲染哪个模板:

<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传入的数据。

更多微信小程序开发教程,可以关注。
上一篇:微信小程序WXML两种文件引用方式,import和include引用方式
下一篇:微信小程序列表渲染,用 wx:for 控制属性绑定一个数组
相关资讯 Releva ntnews
解决方案 Solutions
相关热点 Hot spot
郑州做排名中正确使用关键词优化的方法
  1. 我们的优势
  2. 我们的实力
  3. 选择我们的理由
咨询电话(微信同号)

137-3385-8848(同微信)

豫ICP备16014343号-1

Copyright © 2017-2022 版权所有 酷微科技 Rights Reserved

电话咨询 在线咨询 服务项目 SEO优化