uniapp微信小程序 - 一个页面多个分享button怎么区分(代码整理)

需要点击 ‘邀请好友’ 实现分享当前按钮的商品内容,而且商品内容是接口返回的,其实很简单,刚开始百度了一圈也没个头绪,所以就自己整理下,方便之后翻看

设计图(需求)

在这里插入图片描述

结构

<view v-for="(item, magIndex) in avatarList" :key="id">
	<button open-type="share" :id="magIndex">邀请好友</button>
</view>

js

onShareAppMessage(res) {
			console.log(res)
			return {
				title: '超值商品仅需' + this.avatarList[res.target.id].price + '元~更划算!',
				imageUrl: this.avatarList[res.target.id].picture,
				path: '/pages/xxxx/xxxx?productId='
this.avatarList[res.target.id].productId + '&activityID=' + this.avatarList[res.target.id].actionId
			}
		}
		// this.avatarList	接口返回的数据(类型是数组)
		// res.target.id	当前所点击的button
		// title	分享卡片的标题
		// imageUrl	封面图
		// path	页面路径

注:console.log(res)
在这里插入图片描述

总结

  1. 想实现分享必须在button组件上设置open-type=“share”
  2. 小程序中用户点击分享后,在 onShareAppMessage 处理函数中可以得到该页面的分享信息,也就是上面console出来的res,就可以利用res.target.id(相当于button的索引) 实现想要的功能了。索引在手啥都不怕哈哈哈
Uniapp 中实现微信小程序的多张图片上传,可以按照以下步骤进行操作: 1. 在页面中创建一个按钮,用于触发选择图片的操作。 2. 在按钮的点击事件中,调用 `uni.chooseImage` 方法来选择图片。这个方法会返回选择的图片的临时文件路径。 3. 将选择的图片路径保存在一个数组中。 4. 在页面中展示已选择的图片,可以使用 `v-for` 指令来遍历已选择的图片数组,使用 `uni.previewImage` 方法来预览图片。 5. 创建一个提交按钮,在点击事件中调用 `uni.uploadFile` 方法,将选择的图片上传到服务器。 下面是一个示例代码: ```html <template> <view> <button @tap="chooseImage">选择图片</button> <view v-for="(image, index) in imageList" :key="index"> <image :src="image" mode="aspectFit" @tap="previewImage(index)" /> </view> <button @tap="uploadImages">上传图片</button> </view> </template> <script> export default { data() { return { imageList: [], // 存放已选择的图片路径 }; }, methods: { chooseImage() { uni.chooseImage({ count: 9, // 最多可以选择的图片张数 success: (res) => { // 将选择的图片路径保存到 imageList 数组中 this.imageList = res.tempFilePaths; }, }); }, previewImage(index) { uni.previewImage({ urls: this.imageList, // 需要预览的图片路径列表 current: this.imageList[index], // 当前显示的图片链接 }); }, uploadImages() { // 遍历已选择的图片路径,逐个上传 this.imageList.forEach((image) => { uni.uploadFile({ url: 'https://your-upload-api-url', // 上传图片的接口地址 filePath: image, name: 'file', // 上传文件对应的 key 值 formData: {}, // 其他额外的参数 success: (res) => { console.log(res.data); }, }); }); }, }, }; </script> ``` 请注意替换示例代码中的上传接口地址为你自己的接口地址。另外,需要在 `manifest.json` 文件中添加相应的权限配置,以允许选择图片和上传文件。 希望这个示例对你有帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值