vue element upload组件 file-list的动态绑定

本文解决,upload组件 file-list的动态绑定list1,list2 ...,实现动态添加,相信很多电商后台管理系统都会遇到这个需求,例子如下

123

本例,我是使用的upload默认的上传地址(很多图片不能上传,你可以在本地截几张图片,进行测试),我可以上传多张活动图片,可以加相应的,名称,链接描述等,如果有多个活动,可以点击添加活动,在第二个活动又能添加相应的内容,保存完之后,可以实现回显,活动详情页可以看到添加的几个活动和相应的活动内容。

实现方法不为一,这是一种特别简单的。代码如下

<template>
  <div class="view">
    <div class="item" v-for="(item,index) in formList" :key="index">
      <div style="font-size: 14px; color: #606266;line-height: 40px;">相关图片资料</div>
      <el-upload
        action="https://jsonplaceholder.typicode.com/posts/"
        list-type="picture-card"
        :on-preview="handlePictureCardPreview"
        :on-remove="(file, fileList)=>{return handleRemove(file, fileList, index)}"
        :limit="5"
        :on-exceed="onExceed"
        :file-list="item.pics"
        :on-success="(response, file, fileList)=>{return onSuccess(response, file, fileList, index)}"
      >
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt />
      </el-dialog>
      <el-form label-position="top" label-width="80px" :model="item">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="活动名称">
              <el-input v-model="item.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动链接">
              <el-input v-model="item.content"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-button type="danger" @click="delItem(index)" style="margin-bottom:20px">删除</el-button>
    </div>
    <el-button type="success" @click="addItem" style="width:1000px">添加活动</el-button>
    <el-button type="primary" @click="saveItem" style="margin-top:20px;margin-left:0;">保存活动</el-button>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      formList: [{ pics: [] }]
    };
  },
  methods: {
    // 上传图片
    onSuccess(response, file, fileList, idx) {
      // 这里是element的上传地址,对应的name,url,自己打印fileList对照
      this.formList[idx].pics.push({ name: file.name, url: file.url });
    },
    // 移除图片
    handleRemove(file, fileList, idx) {
      let Pics = this.formList[idx].pics;
      Pics.forEach((item, index) => {
        if (file.name == item.name) {
          Pics.splice(index, 1);
        }
      });
    },
    // 查看图片
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    onExceed(file, fileList) {
      this.$message({
        type: "warning",
        message: "最多上传5张"
      });
    },
    // 添加活动
    addItem() {
      this.formList.push({ pics: [] });
    },
    // 删除活动
    delItem(idx) {
      if (this.formList.length > 1) {
        this.formList.splice(idx, 1);
      } else this.formList = [{ pics: [] }];
    },
    // 保存活动
    saveItem() {
      this.$message({
        type: "success",
        message: "保存成功,可以刷新下试试回显效果"
      });
      console.log(this.formList);
      localStorage.setItem("formList", JSON.stringify(this.formList));
    }
  },
  created() {
    this.formList = JSON.parse(localStorage.getItem("formList"))|| [
      { pics: [] }
    ];;
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.view {
  width: 1000px;
  margin: 0 auto;
}
.item {
  width: 100%;
}
</style>

主要实现,动态添加多个item,每个item都有对应的多张图文和介绍,可以删除,保存,下次进来可以回显继续编辑,详情展示页可以展示

github地址

github预览地址

  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
### 回答1: 在Vue中给el-uploadfile-list添加事件可以通过以下步骤实现: 1. 首先,找到el-upload组件的使用代码,在其中找到file-list部分。 2. 在file-list元素上添加一个自定义属性,用来表示事件触发的方法,例如::on-click="handleClick"。 3. 在Vue实例中定义一个名为handleClick的方法(或者自定义的方法名),该方法将作为事件的处理函数。 4. 在该方法中,可以通过event对象获取到触发事件的相关信息,如文件名、文件大小等。 5. 可以根据需要,在handleClick方法中编写具体的业务逻辑,例如弹出一个提示框、执行一些操作等。 6. 最后,在Vue实例中通过methods选项将handleClick方法添加到Vue实例的方法列表中。 完整的代码示例如下: ```html <template> <el-upload action="" :on-click="handleClick" > <el-button slot="trigger" size="small" type="primary">选取文件</el-button> </el-upload> </template> <script> export default { methods: { handleClick(event) { console.log('文件名:', event.file.name); console.log('文件大小:', event.file.size); // 在这里编写具体的业务逻辑 } } } </script> ``` 通过以上步骤,就可以在Vue中给el-uploadfile-list添加事件,并在事件处理函数中进行相应的操作。 ### 回答2: 在Vue中给el-uploadfile-list添加事件,可以通过使用插槽来实现。 首先,确保已经引入了Element-UI库和Vue。 然后,在Vue的模板中,可以这样定义一个el-upload组件: ```html <template> <div> <el-upload action="/upload" :file-list="fileList" :before-upload="beforeUpload" > <el-button slot="trigger">点击上传</el-button> <el-progress slot="default" :percentage="uploadPercentage"></el-progress> </el-upload> </div> </template> ``` 在通过`:file-list`绑定了一个名为`fileList`的数组,该数组用于存储上传的文件列表数据。同时,通过`:before-upload`属性绑定了一个名为`beforeUpload`的方法,用于在上传文件前执行一些操作。 接下来,需要在Vue的`data`中定义`fileList`和`uploadPercentage`变量,并在`methods`中定义`beforeUpload`方法: ```javascript <script> export default { data() { return { fileList: [], uploadPercentage: 0 }; }, methods: { beforeUpload(file) { // 在这里可以对上传的文件进行一些处理,比如判断文件的类型、大小等 console.log('before upload:', file); // 添加事件 file.on('progress', this.onProgress); }, onProgress(event, file, fileList) { // 上传进度回调,可以在这里更新进度条等操作 console.log('upload progress:', event.percent + '%', file, fileList); this.uploadPercentage = event.percent; } } }; </script> ``` 在`beforeUpload`方法中,可以对上传的文件进行一些处理,比如验证文件类型、大小等。同时,可以通过`file.on('progress', this.onProgress)`添加一个`progress`事件监听器,当文件上传进度发生变化时,将触发`onProgress`方法。 在`onProgress`方法中,可以对上传进度进行一些操作,比如更新进度条等。 通过以上步骤,就可以给el-uploadfile-list添加事件,实现文件上传的监听和处理。 ### 回答3: 在Vue中给el-uploadfile-list添加事件,可以通过自定义组件来实现。以下是一个简单的实现方式: 1. 首先,在Vue组件中引入el-upload组件el-upload的样式文件。 ``` <template> <el-upload class="upload-component" action="/upload" :on-success="handleUploadSuccess" :file-list="fileList" > <el-button slot="trigger" size="small" type="primary">上传文件</el-button> </el-upload> </template> <script> import { ElUpload, ElButton } from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; export default { components: { ElUpload, ElButton }, data() { return { fileList: [] }; }, methods: { handleUploadSuccess(response, file, fileList) { // 处理上传后的逻辑,可以在此处添加与file-list相关的事件 console.log(response); console.log(file); console.log(fileList); } } }; </script> ``` 2. 在模板中引入el-uploadel-button组件,并使用el-upload组件来上传文件。在el-upload上添加了一个handleUploadSuccess方法,用于处理上传成功后的逻辑。 3. 在handleUploadSuccess方法内部,可以获取到上传成功后的响应、上传的文件和当前file-list,可以在此处添加与file-list相关的事件。例如,可以根据响应的内容,动态更新file-list或执行其他操作。 这样,就可以通过自定义组件的方式,在Vue中给el-uploadfile-list添加事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值