Vant Uploader實(shí)現(xiàn)上傳一張或多張圖片組件
本文實(shí)例為大家分享了Vant Uploader實(shí)現(xiàn)上傳一張或多張圖片組件,供大家參考,具體內(nèi)容如下
html部分
<template> <div class="contWrap"> <van-uploader v-model="fileList" :multiple="true" :before-read="beforeRead" :after-read="afterRead" :before-delete="delUploadImg" upload-icon="plus" > <!-- 注:這里是自定義上傳樣式 --> <!-- <p> <van-icon name="plus" color="#07c160" size=".5rem" /> 上傳照片 </p> --> </van-uploader> </div> </template>
js部分
<script> import axios from "axios"; export default { name: "uploadImages", data() { return { fileList: [], uploadUrl: "/api/upload/fileUpload", headers: { token: this.$store.state.account.token, }, }; }, methods: { // 返回布爾值 beforeRead(file) { if (file instanceof Array) { //判斷是否是數(shù)組 file.forEach((v) => { this.checkFile(v); }); } else { this.checkFile(file); } return true; }, //圖片類型檢查 checkFile(file) { const format = ["jpg", "png", "jpeg"]; const index = file.name.lastIndexOf("."); const finalName = file.name.substr(index + 1); if (!format.includes(finalName.toLowerCase())) { Toast("請(qǐng)上傳" + format.join(",") + "格式圖片"); return false; } }, afterRead(file) { // 此時(shí)可以自行將文件上傳至服務(wù)器 if (file instanceof Array) { file.map((v) => { v.status = "uploading"; v.message = "上傳中..."; this.uploadImg(v); }); } else { file.status = "uploading"; file.message = "上傳中..."; this.uploadImg(file); } }, //上傳 uploadImg(file) { const formData = new FormData(); formData.append("file", file.file); axios .post(this.uploadUrl, formData, { headers: this.headers, }) .then((res) => { if (res.data) { if (file instanceof Array) { //判斷是否是數(shù)組 file.map((v, i) => { v.status = "success"; v.message = ""; v.url = res.data[i]; }); } else { file.status = "success"; file.message = ""; file.url = res.data.uploadUrl; } } }) .catch((err) => { this.$notify({ type: "warning", message: "上傳失敗", }); }); }, //刪除 delUploadImg(item) { this.fileList = this.fileList.filter((v) => v.url != item.url); } }, }; </script>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue-cli3項(xiàng)目配置eslint代碼規(guī)范的完整步驟
這篇文章主要給大家介紹了關(guān)于vue-cli3項(xiàng)目配置eslint代碼規(guī)范的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09vue?內(nèi)置組件?component?的用法示例詳解
這篇文章主要介紹了vue內(nèi)置組件component的用法,本文給大家介紹了component內(nèi)置組件切換方法,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08Vue3+Vite使用雙token實(shí)現(xiàn)無(wú)感刷新
本文主要介紹了Vue3+Vite使用雙token實(shí)現(xiàn)無(wú)感刷新,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04vue-cli3環(huán)境變量與分環(huán)境打包的方法示例
這篇文章主要介紹了vue-cli3環(huán)境變量與分環(huán)境打包的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02

Vue3結(jié)合TypeScript項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)記錄

詳解Vue CLI3配置之filenameHashing使用和源碼設(shè)計(jì)使用和源碼設(shè)計(jì)