【微信小程序】template模板使用详解

  WXML提供模板(template),可以在模板中定义代码片段,然后在不同的地方调用。

  模板的作用域:

  模板拥有自己的作用域,只能使用 data 传入的数据以及模板定义文件中定义的 <wxs /> 模块。

定义模板

<template name='allgood-item'>
  <image src='{{icon}}' class='all_item_image'/>
  <view class='all_item_right'>
    <text class='all_item_title'>{{title}}</text>
    <view class='all_item_details'>
      <view>
        <text class='all_item_new'>{{newPrice}}</text>
        <text class='all_item_old'>{{oldPrice}}</text>
      </view>
      <text class='all_item_buy'>立即购买</text>
    </view>
  </view>
</template>

使用模板

<import src='./allgood-item-template/allgood-item-template.wxml'/>

<block wx:for='{{modelArray}}'>
    <template is='allgood-item' data='{{...item}}' />
</block>

  微信小程序结合使用ES6+的解构和属性延展,我们给template传递一批属性更为方便了。

定义模板样式

.all_item_image {
   ...    
}
   ...
.all_item_new,.all_item_old,.all_item_buy{
   ...
}

引用模板样式

@import './allgood-item-template/allgood-item-template.wxss';

template进行绑定事件

<block wx:for='{{modelArray}}'>
  <view class='all_item_view' bindtap='toDetails'>
    <template is='allgood-item' data='{{...item}}' />
  </view>
</block>

 

全部评论

相关推荐

今天 11:06
辽宁大学 市场
深莞高速因为台风都封掉了,华为协商后,特地开通华为通道,凭工卡可以正常通勤......
崔喃喃:“台风您好,19级专家已驳回了您18级台风的OA登陆申请”
投递华为技术有限公司等公司10个岗位
点赞 评论 收藏
分享
牛客48826091...:哥们胸肌挺好看
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 14:18
京✌们还有比我低的吗
迷茫的大四🐶:这工资还能干下去怕不是体制内哦,要不然我想不出来留下来的理由
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务