Skip to content

XAttachment 附件

示例

基础用法

  • food.jpeghttp://dummyimage.com/120x90
  • http://dummyimage.com/200x300food.jpeghttp://dummyimage.com/200x300
  • food.jpeghttp://dummyimage.com/300x300/FF0000

<template>
  <div>
    <XAttachment
      size="small"
      v-model="fileList"
      v-model:select-value="selected"
      :limit="5"
      @change="onChange"
      @click="onClick"
      :uploader="uploader"
      :addable="true"
      clickable
      selectable
      :previewable="true"
      :removable="true"
      :downloadable="true"
      :multiple="true">
    </XAttachment>
  </div>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { XAttachment, type AttachmentFile } from '@vtj/ui';
  import { delay } from '@vtj/utils';

  const fileList = ref<AttachmentFile[]>([
    {
      url: 'http://dummyimage.com/120x90',
      name: 'food.jpeg'
      // type: 'img'
    },
    {
      url: 'http://dummyimage.com/200x300',
      name: 'http://dummyimage.com/200x300food.jpeg'
      // type: 'img'
    },
    {
      url: 'http://dummyimage.com/300x300/FF0000',
      name: 'food.jpeg',
      type: 'img'
    },
    {
      url: 'http://dummyimage.com/300x300'
      // name: 'food.docx'
      // type: 'word'
    }
  ]);

  const selected = ref({
    url: 'http://dummyimage.com/120x90'
  });

  const onChange = (files: any) => {
    console.log('onChange', files);
  };

  const onClick = (file: any) => {
    console.log('click', file);
  };

  const uploader: any = async () => {
    await delay(1000);
    // return null;
    return 'https://oss.newpearl.com/newpearl/image/2024-07-15/acd6ff3e0bf8fce74d795a870c9069e6.png';
  };
</script>

模式用法

列表模式

  • food.jpeghttp://dummyimage.com/120x90
  • http://dummyimage.com/200x300food.jpeghttp://dummyimage.com/200x300
  • food.jpeghttp://dummyimage.com/300x300/FF0000
  • http://dummyimage.com/300x300

卡片模式

  • food.jpeghttp://dummyimage.com/120x90
  • http://dummyimage.com/200x300food.jpeghttp://dummyimage.com/200x300
  • food.jpeghttp://dummyimage.com/300x300/FF0000
  • food.jpeghttp://dummyimage.com/120x90
  • http://dummyimage.com/200x300food.jpeghttp://dummyimage.com/200x300
  • food.jpeghttp://dummyimage.com/300x300/FF0000

<template>
  <div>
    <h1>列表模式</h1>
    <XAttachment
      size="large"
      list-type="list"
      v-model="fileList"
      v-model:select-value="selected"
      @change="onChange"
      @click="onClick"
      :uploader="uploader"
      :addable="true"
      clickable
      selectable
      :previewable="true"
      :removable="true"
      :downloadable="true"
      :multiple="true"></XAttachment>
    <hr />
    <h1>卡片模式</h1>
    <XAttachment v-model="fileList"></XAttachment>
    <XAttachment size="large" v-model="fileList"></XAttachment>

    <XAttachment
      multiple
      :formatter="formatter"
      :valueFormatter="valueFormatter"
      :uploader="uploader"
      v-model="files"
      :auto-upload="true"
      style="
        border: 1px red solid;
        display: flex;
        justify-content: center;
      "></XAttachment>
  </div>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { XAttachment, type AttachmentFile } from '@vtj/ui';
  import { delay } from '@vtj/utils';

  const fileList = ref<AttachmentFile[]>([
    {
      url: 'http://dummyimage.com/120x90',
      name: 'food.jpeg'
    },
    {
      url: 'http://dummyimage.com/200x300',
      name: 'http://dummyimage.com/200x300food.jpeg'
    },
    {
      url: 'http://dummyimage.com/300x300/FF0000',
      name: 'food.jpeg',
      type: 'img'
    },
    {
      url: 'http://dummyimage.com/300x300'
    }
  ]);

  const selected = ref({
    url: 'http://dummyimage.com/120x90'
  });

  const onChange = (files: any) => {
    console.log('onChange', files);
  };

  const onClick = (file: any) => {
    console.log('click', file);
  };

  const uploader: any = async () => {
    await delay(1000);
    return 'https://oss.newpearl.com/newpearl/image/2024-07-15/acd6ff3e0bf8fce74d795a870c9069e6.png';
  };

  const formatter: any = async (files: any) => {
    console.log('formatter', files);
    for (const file of files) {
      file.__url = file.url;
      file.url = file.url + '?only=true';
    }
    return files;
  };

  const valueFormatter: any = async (files: any) => {
    console.log('valueFormatter', files);
    return files.map((n: any) => {
      return {
        url: n.url,
        name: n.name
      };
    });
  };

  const files = ref([]);
</script>

插槽用法

  • food.jpeghttp://dummyimage.com/120x90
  • http://dummyimage.com/200x300food.jpeghttp://dummyimage.com/200x300
  • food.jpeghttp://dummyimage.com/300x300/FF0000
提示文字提示文字提示文字提示文字提示文字

<template>
  <div>
    <XAttachment
      size="small"
      v-model="fileList"
      v-model:select-value="selected"
      :limit="5"
      @change="onChange"
      @click="onClick"
      :uploader="uploader"
      :addable="true"
      clickable
      selectable
      :previewable="true"
      :removable="true"
      :downloadable="true"
      :multiple="true">
      <template #tip>
        <div>提示文字提示文字提示文字提示文字提示文字</div>
      </template>
    </XAttachment>
  </div>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { XAttachment, type AttachmentFile } from '@vtj/ui';
  import { delay } from '@vtj/utils';

  const fileList = ref<AttachmentFile[]>([
    {
      url: 'http://dummyimage.com/120x90',
      name: 'food.jpeg'
    },
    {
      url: 'http://dummyimage.com/200x300',
      name: 'http://dummyimage.com/200x300food.jpeg'
    },
    {
      url: 'http://dummyimage.com/300x300/FF0000',
      name: 'food.jpeg',
      type: 'img'
    },
    {
      url: 'http://dummyimage.com/300x300'
    }
  ]);

  const selected = ref({
    url: 'http://dummyimage.com/120x90'
  });

  const onChange = (files: any) => {
    console.log('onChange', files);
  };

  const onClick = (file: any) => {
    console.log('click', file);
  };

  const uploader: any = async () => {
    await delay(1000);
    return 'https://oss.newpearl.com/newpearl/image/2024-07-15/acd6ff3e0bf8fce74d795a870c9069e6.png';
  };
</script>

API

属性

属性名说明类型默认值
modelValue列表显示的文件array-
selectValue选中值,开启 selectable 有效object | array-
uploader文件上传方法function-
multiple支持多文件上传, 同时在selectable时控制多选boolean-
limit允许上传文件的最大数量number-
accept接受上传的文件类型string'image/* ,audio/* ,video/*,.zip,.svg,.pdf,.json,.docx,.xlsx,.pptx,.doc,.xls,.ppt'
disabled禁止更改文件,不能上传和删除boolean-
size图片显示尺寸stringdefault
thumbnail缩略图生成方法function-
addable可增加booleantrue
removable可删除booleantrue
downloadable可下载booleantrue
previewable可预览booleantrue
selectable可选择booleanfalse
clickable可点击booleanfalse
listType列表类型string<card | list>card
beforeUpload上传前守卫function-
limitSize允许上传的文件大写最大值, 支持 K / Mstring10M
formatter格式化function-
valueFormatter列表数据格式化function-
previewer预览器方法function-
downloader下载器方法function-
autoUpload是否自动上传文件booleantrue

事件

名称说明参数
select选择文件上传-
click点击文件-
change文件数据改变-
remove删除-
download下载-
preview预览-

插槽

插槽名说明类型
tip提示信息-

Released under the MIT License.