参考博客:uni-app 图片上传 demo
样式部分:
<view class="img-flex">
<block v-for="(image,index) in imageList" :key="index">
<image class='image-class' :src="image?image:defaultImg" mode="aspectFit" @click="selImg(index)"></image>
</block>
</view>
主要方法:uni.chooseImage()
selImg: async function(index) {
uni.chooseImage({
count: 1,
success: (res) => {
this.imageList[index] = res.tempFilePaths[0]
this.$forceUpdate() //强制刷新视图
}
})
}
注: (1)回调函数用(res) => {}格式,否则需要另外定义this指向
(2)Vue中视图不一定随数组重新赋值而更新,可以强制刷新视图 this.$forceUpdate()