Vue开发之封装上传文件组件与用法示例
2019-04-25 18:01
886 查看
本文实例讲述了Vue开发之封装上传文件组件与用法。分享给大家供大家参考,具体如下:
使用elementui的 el-upload插件实现图片上传组件
每个项目存在一定的特殊性,所以数据的处理会不同
pictureupload.vue:
<template> <div class="pictureupload"> <el-upload :action="baseUrl + '/api/public/image'" list-type="picture-card" :on-preview="handlePictureCardPreview" :on-remove="handleRemove" :file-list="fileList" :on-exceed="handleExceed" :before-remove="beforeRemove" name="img" :on-success="upLoadSuccess" :data="upLoadData" :headers="headers" :limit="limit"> <i class="el-icon-plus"></i> </el-upload> <el-dialog :visible.sync="dialogVisible"> <img width="100%" :src="dialogImageUrl" alt=""> </el-dialog> </div> </template> <script> import { getToken } from "@/utils/auth"; import store from "@/store"; export default { props: { imgList: { type: Array, default: [] }, // 父组件传递的图片列表 limit: "" // 图片数量限制 }, data() { return { fileList: [], upLoadData: { img: "" }, baseUrl: process.env.BASE_API, dialogVisible: false, dialogImageUrl: "", headers: { Authorization: store.getters.token_type + " " + store.getters.token } // 接口调用token }; }, watch: { // 监听imgList的变化: fileList要求的格式为[{url: img}],所以监听imgList变化后将其进行处理,赋值给fileList imgList: { handler(newValue, oldValue) { if(newValue.length === 0) { this.fileList = []; return; } for (let i = 0; i < newValue.length; i++) { if (oldValue[i] != newValue[i]) { this.fileList = []; newValue.forEach(el => { this.fileList.push({url: el}) }) return; } } }, deep: true } }, methods: { // 图片移除时处理数据 handleRemove(file, fileList) { let item = []; fileList.forEach(el => { item.push(el.url); }); this.$emit("removeimg", item); }, handlePictureCardPreview(file) { this.dialogImageUrl = file.url; this.dialogVisible = true; }, // 判断图片数量 handleExceed(files, fileList) { this.$message.warning(`当前限制选择 ${this.limit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`); }, beforeRemove(file, fileList) {}, // 上传图片成功事件 upLoadSuccess(response) { this.$emit("uploadimg", response.message); this.$message("上传成功",); } }, mounted() { if (this.imgList.length != 0) { this.imgList.forEach(el => { this.fileList.push({ url: el }); }); } } }; </script>
使用上传图片组件:
<pictureupload @uploadimg="uploadimg" :imgList="ruleForm.img" :limit="3" @removeimg="removeimg"></pictureupload>
removeimg(item) { this.ruleForm.img = item; }, uploadimg(item) { this.ruleForm.img.push(item); },
希望本文所述对大家vue.js程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- Vue封装一个简单轻量的上传文件组件的示例
- vue webuploader 文件上传组件开发
- Vue上传组件vue Simple Uploader的用法示例
- 基于vue-upload-component封装一个图片上传组件的示例
- 【转】Asp.NET大文件上传组件开发总结(四)---封送数据给Asp.NET页面
- 使用apache commons 2.0 net组件开发ftp上传大文件使程序假死
- 【转】Asp.NET大文件上传组件开发总结(三)---处理文件内容
- jquery组件WebUploader文件上传用法详解
- Vue上传文件 iview Upload UI 组件上传组件
- 【转载】Asp.NET大文件上传组件开发总结(二)---提取文件内容
- 如何开发一个java开源框架-----Jvn框架之实现文件的上传以及方法封装(第六讲)八哥实力讲解
- vuejs+element-ui+laravel5.4上传文件的示例代码
- vue脚手架搭建之单文件组件的开发
- Asp.NET大文件上传组件开发总结(五)---上传进度信息的显示
- 文件上传组件 开发
- java组件commons-fileupload文件上传示例
- VUE开发一个图片轮播的组件示例代码
- Vue.js弹出模态框组件开发的示例代码
- Android开发高级组件之自动完成文本框(AutoCompleteTextView)用法示例【附源码下载】
- Asp.NET大文件上传组件开发总结(三)---处理文件内容