uni-app vue2.0运行到微信开发者工具中点击事件传入参数接收不到

uni-app vue2.0运行到微信开发者工具中点击事件传入参数接收不到

开发环境 版本号 项目创建方式
Windows 11 HBuilderX

操作步骤:

  • 点击事件 openClick 打印 item 显示为 undefined
  • 在 HBuilderX 4.45 中也有这个问题

预期结果:

  • 事件 openClick 打印 item

实际结果:

  • 点击事件 openClick 打印 item 显示为 undefined
  • 在 h5 中正常
<template> <view class="page-min hospital-view column px-32"> <view class="u-bdr-16 bg-f w-100 space-between u-m-t-20 u-p-20" v-for="(item,index) in list" :key="item.id" @click="openClick(item)"> <u-image width="110rpx" height="110rpx" border-radius="10" :src="item.logo_img"></u-image> <view class="u-flex-1 u-m-l-12"> <view class="u-f-30 font-weight-550">{{ item.shop_name }}</view> <view class="u-m-t-12 u-flex"> <view class="u-flex-1 u-f-24 w-s-color-6">{{ item.address }}</view> <view class="u-m-l-14 u-flex u-f-24 text-primary"> <image src="@/static/icon/19.png" class="address-img" mode=""></image> <text>{{ item.distance_unit }}</text> </view> </view> </view> </view> </view> </template> <script> import api from '@/api/shop.js'; export default { data() { return { /* 列表相关 */ loading: 'loading', refreshFlag: true, list: [], total: 0, query: { page: 1, latitude: '30.237885', longitude: '120.209764', city_id: '' }, cityShow: false, cityText: '杭州', cityList: [] }; }, onShow() { this.refresh(); }, onPullDownRefresh() { this.refresh(); }, onReachBottom() { if (this.loading == 'loadmore') { this.query.page += 1; this.getList(); } }, methods: { /* 打开地图 */ openClick(item) { console.log(item); return uni.openLocation({ latitude: Number(item.latitude), longitude: Number(item.longitude), success: () => {}, fail: (err) => { console.log(err); } }); }, /** * 刷新 */ refresh() { this.refreshFlag = true; this.query.page = 1; uni.showLoading({ title: '加载中' }); this.getList(); }, getList() { this.loading = 'loading'; api.getHospitalList(this.query).then((res) => { let { data, total } = res.data.list; if (this.refreshFlag) { this.list = data; uni.hideLoading(); this.refreshFlag = false; uni.stopPullDownRefresh(); } else { this.list = [...this.list, ...data]; } this.total = total; this.loading = this.list.length < total ? 'loadmore' : 'nomore'; }); } } }; </script> <style lang="scss" scoped> .hospital-view { padding-bottom: calc(20rpx + constant(safe-area-inset-bottom)); padding-bottom: calc(20rpx + env(safe-area-inset-bottom)); .address-img { width: 28rpx; height: 28rpx; margin-right: 4rpx; } .city-view { padding: 34rpx 28rpx; .city-scoll { max-height: 540rpx; .city-all { overflow: hidden; .city-tag { padding: 0 28rpx 30rpx 0; float: left; width: 25%; .tag-view { padding: 16rpx 20rpx; text-align: center; border-radius: 16rpx; border: 2rpx solid rgba(170, 170, 170, 0.38); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 24rpx; transition: all 0.3s; } .active-tag { border-color: #ef1b49; color: #ef1b49; } } } } } } </style>

更多关于uni-app vue2.0运行到微信开发者工具中点击事件传入参数接收不到的实战教程也可以访问 https://www.itying.com/category-93-b0.html

10 回复

改成openClick(index,item)
openClick(index,item) { console.log(item); console.log(index); return uni.openLocation({ latitude: Number(item.latitude), longitude: Number(item.longitude), success: () => {}, fail: (err) => { console.log(err); } }); },

index能够拿到,item始终拿不到,整个项目就这一个页面出现了这个问题

更多关于uni-app vue2.0运行到微信开发者工具中点击事件传入参数接收不到的实战教程也可以访问 https://www.itying.com/category-93-b0.html


hello , 我这里不能直接运行你的代码,因此无法直接复现,但是从你的代码中,可能的原因是绑定问题或者是盒子大小等因素,可以提供一下一个可以复现的项目吗?

项目目前没上线

回复 xyj199509: 可以新建一个hello项目,将代码copy过去,然后数据写死看看

你好,方便提供微信号吗!我给你拉到体验版

回复 恭喜n发财: 好的,我试试

回复 恭喜n发财: 能出来

写死数据后原来的项目也能出来,从后端请求数据后就出不来了

这是一个在uni-app中微信小程序环境下的事件参数传递问题。从代码来看,H5端正常但微信小程序端获取不到参数,可能的原因和解决方案如下:

  1. 微信小程序环境的事件对象处理差异:
  • 在微信小程序中,事件参数会被包装在事件对象中,建议修改事件处理为:
openClick(e) {
    const item = e.currentTarget.dataset.item;
    console.log(item);
}
  1. 修改模板中的事件绑定方式:
<view @click="openClick" :data-item="item"></view>
  1. 临时解决方案(兼容写法):
openClick(item) {
    if(!item && arguments[0].currentTarget) {
        item = arguments[0].currentTarget.dataset.item;
    }
    console.log(item);
}
回到顶部