首先,在data里面定义一个参数,用于存放图片缓存参数
我在这里定义了一个list的数组,用于存放参数
在页面中添加一个按钮
<view>
<text>上传图片:</text>
<button type="default" bindtap="onAddImg">添加</button>
</view>
设置一个bindtap的方法名为onAddImg
在js文件中添加对应的方法
onAddImg() {
const _this = this
wx.chooseMedia({
count: 9, // 图片最多选择数量
mediaType: ["image"],
sizeType: ["compressed"],// 压缩图表质量
success: function (res) {
console.log(res)
_this.setData({
list: res.tempFiles
})
}
})
}
这里的this 需要改变一下指向 res 里面携带的参数就是缓存在本地的图片信息
将本地缓存的图片渲染到页面
<view>
<block wx:for="{{list}}" wx:key="key" wx:for-item="img">
<image src="{{img.tempFilePath}}" class="mapImg"></image>
</block>
<image src="../../img/addImg.png" class="addImg"></image>
</view>
这里需要注意微信小程序的模板语法与vue的模板语法是有差异的,需要按照官方的格式来才行
渲染效果
本地缓存的数据是这样的
END
主要是注意模板语法的渲染,与this的指向问题 ,还有一个就是用 wx.chooseMedia方法,而不是用之前老旧的方法( wx.chooseImage),老旧的方法官方已经是不推荐了。