Skip to content

引入组件

局部引入

ts
// parent.vue
const FUpload = defineAsyncComponent(() => import('@/components/formaui/f-upload/f-upload.vue'))

基础用法

v-model属性:图片地址列表

html
<f-upload v-model="images" @complete="onComplete" @remove="onRemove" />
ts
import { ref } from 'vue'

const images = ref<string[]>([])

const onComplete = (detail: { status: number; imgArr: string[] }) => {
  console.log(detail.status, detail.imgArr)
}

const onRemove = (detail: { index: number }) => {
  console.log(detail.index)
}

limit属性:最大上传数量(默认9

html
<f-upload v-model="images" :limit="3" />

serverUrl属性:上传接口地址

设置后选择图片将自动上传;接口返回格式需为 { status: 200, data: '图片地址' }status % 100 === 0 视为成功)。

html
<f-upload v-model="images" server-url="https://api.example.com/upload" />

isMultiple属性:是否多选(默认false

html
<f-upload v-model="images" is-multiple :limit="9" />

isDelConfirm属性:删除前弹框确认(默认false

html
<f-upload is-del-confirm />

isForbidDel / isForbidAdd属性:禁用删除或添加

html
<f-upload is-forbid-del />
<f-upload is-forbid-add />

borderRadius / borderColor / borderStyle属性:添加按钮样式

html
<f-upload border-radius="16rpx" border-color="var(--f-border-color)" border-style="dashed" />

isCenter属性:垂直居中排列(默认false

html
<f-upload is-center />

width / height属性:图片展示尺寸(默认218rpx

html
<f-upload width="160rpx" height="160rpx" />

插槽

名称说明
default自定义添加按钮

属性

名称类型说明默认值
v-modelstring[]图片地址列表[]
widthstring展示图片宽度218rpx
heightstring展示图片高度218rpx
borderRadiusstring圆角半径0
bgColorstring添加按钮背景色#F7F7F7
isCenterboolean是否垂直居中排列false
borderColorstring添加按钮边框颜色,transparent 无边框transparent
boxShadowstring添加按钮阴影none
borderStylestring边框样式,可传入[soliddasheddotted]dashed
delColorstring删除按钮背景色rgba(0,0,0,0.6)
isDelConfirmboolean删除前是否弹框确认false
isForbidDelboolean是否禁用删除false
addColorstring添加图标颜色#888888
addSizestring添加图标大小68rpx
isForbidAddboolean是否禁用添加false
serverUrlstring上传接口地址,为空时仅返回本地路径-
limitnumber最大上传数量9
isMultipleboolean是否支持多选false
sizeType('original' | 'compressed')[]图片尺寸类型原图与压缩图均有
sourceType('album' | 'camera')[]图片来源相机与相册均有
imageFormatstring[]允许的图片格式,为空不限制[]
maxSizenumber单张图片大小上限(MB)4
fileKeyNamestring上传文件对应的 formData keyfile
headerRecord<string, string>HTTP 请求 Header{}
formDataRecord<string, any>HTTP 请求额外的 formData{}
paramsnumber | string自定义参数,在事件中回传-

事件

事件名说明回调参数
complete上传状态变化时触发{ status, imgArr, params?, manual? },status:1 成功 2 上传中 3 失败
remove删除图片时触发{ index, params? }
reupload点击重新上传时触发{ index }
click点击添加按钮时触发-

方法

通过 ref 调用组件实例方法:

方法名说明传入参数
uploadAllImage一次性上传所有未上传的本地图片serverUrl: string 接口地址
upload使用自定义 Promise 上传callback: (file) => Promise<string>index?: number