在小程序当中处理用户的逻辑交互大概有如下步骤
小程序事件的绑定是通过 bind 关键字实现的,我们直接在指定的组件上绑定上述的事件即可
eg: wxml
<view bindtap="onTap"> 点击我 view>
<view bind:tap="onTap"> 点击我 view>
两种绑定方式没有任何区别
js
Page({
data: {
},
function (options) {
},
// 这里编写你的回调函数,我这里实现了一个简单的页面跳转功能
onTap: function() {
console.log("你点击了 tap");
wx.navigateTo({
url: '/pages/posts/posts',
success : function() {
console.log("跳转成功");
},
error: function() {
console.log("服务器出现错误");
}
})
}
})
补充: 两种页面跳转的方式的区别
区别一:两种页面跳转的方式的区别就是左上角是否能够返回到上一页
区别二:
catch 和 bind 的区别在于 事件冒泡的区别
在事件的捕捉中可以使用 bind 以及 catch,下面以简单的实例给大家展示一下事件冒泡
wxml
<view class="outter" bindtap="onTap">
<view class="inner" bindtap="onTap1">我是内部view>
view>
js
Page({
onLoad: function (options) {
},
onTap: function() {
console.log("outter 被点击了");
},
onTap1:function() {
console.log("inner 被点击了");
}
})
效果图:
当我点击内圈的时候,两个绑定事件都会被触发,这个时候就发生了冒泡事件
为了避免出现这样的情况,就出现了 catch ,来解决出现事件冒泡的问题,这里我把两个 bind 改成 catch,然后再分别点击内圈和外圈,这样就避免出现了事件冒泡的问题
在真实的开发环境当中,小程序的数据是来自服务端的,如果我们把所有的数据都写在一个 js 文件当中,显然是不太可能的,这里采用模拟服务端的数据,渲染到前端
然后采用数组对象的形式,把数据全部集中起来
data.js
var test = [
{
title:"xxx",
description:"xxx",
pub_date: "xxx",
content: "xxx"
},
{
title:"xxx",
description:"xxx",
pub_date: "xxx",
content: "xxx"
},{
title:"xxx",
description:"xxx",
pub_date: "xxx",
content: "xxx"
}
// 重点来了,这里要把文件导出去给其他的 js 脚本使用
module.exports = {
test: test
}
// 导入数据
var test= require("../../data/test.js")
Page({
data: {
},
onLoad: function(options) {
// 页面加载时,从服务器取数据,这里模拟取数据,使用数据模拟的方式传数据
// 传数据
this.setData({
// 这里根据代码的实际情况进行适当的改变即可
test: test
});
},
})
template 支持 wxml 和 wxss 复用,而不能支持 js 和 json
比如我要复用这样的代码块
把这部分的代码块单独放进一个 template 目录下的 wxml 文件中,name 设置为 postItem ,样式我就不复制了
<template name="postItem">
<view class="post-container">
<view class="post-author-date">
<image class="post-author" src="{{item.author_img}}">image>
<text class="post-date">{{item.date}}text>
view>
<text class="post-title">{{item.title}}text>
<image class="post-image" src="{{item.post_img}}">image>
<text class="post-content">
{{item.content}}
text>
<view class="post-like">
<image class="post-like-image" src="../../images/icon/chat.png">image>
<text class="post-like-image">{{item.view_num}}text>
<image class="post-like-image" src="../../images/icon/view.png">image>
<text class="post-like-image">{{item.collect_num}}text>
view>
view>
template>
然后回到需要复用的地方
.
,这个路径可以是 绝对路径,可以是相对路径,但是建议使用 相对路径 <block wx:for="{{post_content}}" wx:key="{{this}}">
<template is="postItem" data="{{item}}" />
block>
@import "模板样式路径"
需要特别注意的一点,路径最容易错误的,这里一定要小心
模板中不能使用 js 文件,因此业务职能写在引用文件的 js 中
template 的引用记住使用绝对路径
假定我们要实现如下业务,我们在一个 for 循环实现的新闻列表要做到点击哪一个新闻就会具体显示该新闻描述,在这种情况,我们就需要通过每条新闻的下标显示指定数据
在view 标签中就可以使用,data-xxx 指定相对应的属性,然后使用 插值表达式绑定一个唯一的 id
<view catch:tap="onPostTap" data-post_id="{{item.post_id}}">
<template is="postItem" data="{{item}}" />
view>
我们使用的是 bindtap 的点击事件,所以他会传一个值 event,通过 event.currentTarget.dataset.post_id 就可以获取到值了
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有