微信小程序 template添加点击事件

介绍

template是微信小程序提供的模板,可以在模板中定义代码片段,然后在不同的地方调用。

简单使用

定义template

因为项目中可能会需要到不止一个template,所以最好新建一个文件夹来存放template相关的文件。注意这里只是单独的创建各个文件,并不是创建Page或者Component.
创建好之后的文件目录如图:
这里写图片描述

这里演示一个用template当列表的item,然后可以点击并获取到值。
然后开始写templates.wxml文件,这里面可以有多个template代码块,如代码所示:

<!--列表页item  -->
<template name="template01">
  <view class='item' bindtap='onclick' data-item='{{item}}'>
    {{item}}
  </view>
</template>

<template name="template02">
  <view>
    this is template02 and nothing to do
  </view>
</template>

可以看出template和普通的标签定义差不多,但是每个template必须为它设置name,因为在使用的时候可以是根据这个name的值来找到对应关系的。

然后开始布局,templates.wxss文件代码:

.item{
  background: beige;
  padding: 10px;
  display: flex;
  flex: 1;
  justify-content: center;
  align-items: center;
  margin: 20px 10px 10px 10px
}

最后完成template里面的逻辑事件,templates.js文件代码:

var temp = {
  onclick: function (event) {
    console.log("点击了" + event.currentTarget.dataset.item)
  }
}
//导出,供外部使用
export default temp
使用template

经过上面几步template的定义工作就基本完成了,下面开始具体的使用,首先在需要使用template的文件中引入templates.wxml文件,list.wxml文件的具体代码:

<!--引入wxml文件  -->
<import src="../templates/templates.wxml"/>

<view wx:for="{{arrys}}" wx:key="">
  <view class='item' bindtap='itemclick' data-item='{{item}}'>
    <!--和普通标签一样使用,is对应的是templates中的name  data是传入template值  -->
    <template is="template01" data="{{item}}"/>
  </view>
</view>

然后在list.wxss中引入template中用到的样式。

@import "../templates/templates.wxss";

最后在list.js中引入templates的js文件:

import templates from '../templates/templates'

到这里templates作为列表的item就可以显示了,那具体的点击事件是怎么处理的呢?
我们看这一段:

<view class='item' bindtap='itemclick' data-item='{{item}}'>
    <!--和普通标签一样使用,is对应的是templates中的name  data是传入template值  -->
    <template is="template01" data="{{item}}"/>
</view>

给template的外层view设置了一个点击事件,点击事件的具体代码:

  itemclick(event){
    templates.onclick(event)
  }

其实外层view的点击事件自己什么也没做,只是调用了template中的点击事件onclick,并且把当前事件传递给了它。

  onclick: function (event) {
    console.log("点击了" + event.currentTarget.dataset.item)
  }

这样template的点击事件和传值就实现了。
效果图:

这里写图片描述

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
实现微信小程序点击按钮出现弹窗的步骤如下: 1. 在 wxml 文件中添加一个按钮组件,并为其绑定一个点击事件,例如: ```html <button bindtap="showModal">点击打开弹窗</button> ``` 2. 在 js 文件中定义 showModal 函数,用于控制弹窗的显示和隐藏。可以使用 wx.showModal() 函数来显示弹窗,例如: ```javascript Page({ showModal: function() { wx.showModal({ title: '提示', content: '这是一个弹窗', success: function(res) { if (res.confirm) { console.log('用户点击确定') } else if (res.cancel) { console.log('用户点击取消') } } }) } }) ``` 3. 在样式文件中定义弹窗的样式,例如: ```css /** 弹窗蒙层 **/ .modal-mask { position: fixed; top: 0; left: 0; z-index: 1000; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); } /** 弹窗内容 **/ .modal { position: fixed; top: 50%; left: 50%; z-index: 1001; transform: translate(-50%, -50%); background-color: #fff; padding: 20px; border-radius: 5px; } ``` 4. 在 wxml 文件中添加弹窗的模板,例如: ```html <template name="modal"> <view class="modal-mask"> <view class="modal"> <view class="modal-title">{{title}}</view> <view class="modal-content">{{content}}</view> <view class="modal-buttons"> <button class="modal-button" bindtap="confirm">确定</button> <button class="modal-button" bindtap="cancel">取消</button> </view> </view> </view> </template> ``` 5. 在 js 文件中定义 confirm 和 cancel 函数,用于处理用户点击确定和取消的事件,例如: ```javascript Page({ data: { modal: { title: '提示', content: '这是一个弹窗' } }, showModal: function() { this.setData({ modalVisible: true }) }, confirm: function() { console.log('用户点击确定') this.setData({ modalVisible: false }) }, cancel: function() { console.log('用户点击取消') this.setData({ modalVisible: false }) } }) ``` 6. 在 wxml 文件中使用模板,例如: ```html <button bindtap="showModal">点击打开弹窗</button> <template is="modal" data="{{...modal, modalVisible: modalVisible}}" /> ``` 通过以上步骤实现微信小程序点击按钮出现弹窗的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值