写一个微信小程序使用uploader上传图片和视频将数据放进一个数组里传给父组件详细代码
时间: 2023-12-10 18:39:03 浏览: 160
以下是一个简单的微信小程序,可以使用uploader上传图片和视频,并将数据放入数组中传递给父组件。
1. 在wxml文件中添加上传组件:
```
<view class="uploader">
<uploader
maxCount="9"
bind:afterRead="onAfterRead"
/>
</view>
```
2. 在js文件中定义一个数组来存储上传的文件:
```
data: {
fileList: []
},
```
3. 在js文件中添加afterRead方法,在该方法中将上传的文件添加到数组中:
```
onAfterRead(event) {
const { file } = event.detail;
const fileList = this.data.fileList;
fileList.push(file);
this.setData({
fileList
});
}
```
4. 在父组件中使用子组件,并绑定一个自定义事件,将数组传递给父组件:
```
<!-- 子组件 -->
<custom-uploader bind:upload="onUpload" />
// 父组件中的方法
onUpload(event) {
const fileList = event.detail.fileList;
console.log(fileList);
}
```
完整代码如下:
wxml文件
```
<view class="uploader">
<uploader
maxCount="9"
bind:afterRead="onAfterRead"
/>
</view>
```
js文件
```
data: {
fileList: []
},
onAfterRead(event) {
const { file } = event.detail;
const fileList = this.data.fileList;
fileList.push(file);
this.setData({
fileList
});
this.triggerEvent('upload', { fileList });
}
```
父组件wxml文件
```
<!-- 子组件 -->
<custom-uploader bind:upload="onUpload" />
```
父组件js文件
```
onUpload(event) {
const fileList = event.detail.fileList;
console.log(fileList);
}
```
阅读全文
相关推荐


















