首页 > 解决方案 > 如何删除 vuetify 上传的 3 个文件中的 1 个文件?

问题描述

我在文档上阅读:https ://vuetifyjs.com/en/components/file-inputs#selection-slot

我的代码是这样的:

<v-file-input
  v-model="files"
  placeholder="Upload your documents"
  label="File input"
  multiple
  prepend-icon="mdi-paperclip"
>
  <template v-slot:selection="{ text }">
    <v-chip
      small
      label
      color="primary"
    >
      {{ text }}
    </v-chip>
  </template>
</v-file-input>

我的代码笔: https ://codepen.io/positivethinking639/pen/vYONqKa ?editable=true&editors=101

上传 3 张图片并删除时,将删除所有图片。我希望用户能够根据自己的选择删除 1 张图片。所以用户可以删除1张图片

我该怎么做?

标签: vue.jsfile-uploadvue-componentvuetify.jsimage-uploading

解决方案


使用处理程序方法配置要删除的芯片。

  • 添加“关闭”属性以v-chip在每个文件上获取关闭按钮

  • 将处理程序添加到芯片,传递索引(如果要提示,还可以传递文本)

  • (可选)删除 VFileInput 上的全部清除按钮以防止用户混淆

模板

<v-file-input
  ...
  :clearable="false"
>
  <template v-slot:selection="{ index, text }">
    <v-chip
      ...
      close
      @click:close="deleteChip(index, text)"
    >
      {{ text }}
    </v-chip>
  </template>

零件

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: () => ({
    files: [],
  }),
  methods: {
    deleteChip(index, text) {
      // Prompt here with text if required
      this.files.splice(index, 1)
    },
  }
})

推荐阅读