zoukankan      html  css  js  c++  java
  • Ant Design Vue照片墙a-upload

    <template>
        <div class="clearfix">
            {{ fileList }}
    
            <a-upload
                list-type="picture-card"
                v-model:file-list="fileList"
                @preview="handlePreview"
                @change="handleChange"
                :customRequest="customRequest"
            >
                <div v-if="fileList.length < 8">
                    <plus-outlined />
                    <div class="ant-upload-text">Upload</div>
                </div>
            </a-upload>
    
            <!-- 
                回填
            -->
            <a-modal
                :visible="previewVisible"
                :footer="null"
                @cancel="handleCancel"
            >
                <img alt="example" style=" 100%" :src="previewImage" />
            </a-modal>
    
            <button @click="showWrite">回填</button>
        </div>
    </template>
    <script lang="ts">
    import { PlusOutlined } from '@ant-design/icons-vue'
    import { defineComponent, ref } from 'vue'
    
    function getBase64(file: File) {
        return new Promise((resolve, reject) => {
            const reader = new FileReader()
            reader.readAsDataURL(file)
            reader.onload = () => resolve(reader.result)
            reader.onerror = error => reject(error)
        })
    }
    
    export default defineComponent({
        components: {
            PlusOutlined,
        },
        setup() {
            // 全屏预览状态
            const previewVisible = ref<boolean>(false)
            // 预览的照片
            const previewImage = ref<string | undefined>('')
            // 存放显示的图片数组集合
            const fileList: any = ref([])
            // 取消预览
            const handleCancel = () => {
                previewVisible.value = false
            }
            // 打开预览
            const handlePreview = async file => {
                if (!file.url && !file.preview) {
                    file.preview = (await getBase64(file.originFileObj)) as string
                }
                previewImage.value = file.url || file.preview
                previewVisible.value = true
            }
    
            // customRequest 可以解决一个问题
            function customRequest(mess) {
                console.log(mess)
            }
    
            // 更改文件上传的的状态,将uploading状态更改为done完成状态哈
            function handleChange(mess) {
                mess.file.status = 'done'
            }
    
            // 模拟数据回填
            function showWrite() {
                fileList.value = [
                    {
                        // uid: '-1',
                        // name: 'image.png',
                        // status: 'done',
                        url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
                    },
                    {
                        // uid: '-2',
                        // name: 'image.png',
                        // status: 'done',
                        url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
                    },
                ]
            }
            return {
                previewVisible,
                previewImage,
                fileList,
                showWrite,
                handleCancel,
                handlePreview,
                customRequest,
                handleChange,
            }
        },
    })
    </script>
    <style>
    /* you can make up upload button and sample style by using stylesheets */
    .ant-upload-select-picture-card i {
        font-size: 32px;
        color: #999;
    }
    
    .ant-upload-select-picture-card .ant-upload-text {
        margin-top: 8px;
        color: #666;
    }
    </style>
    
    
    作者:明月人倚楼
    出处:https://www.cnblogs.com/IwishIcould/

    想问问题,打赏了卑微的博主,求求你备注一下的扣扣或者微信;这样我好联系你;(っ•̀ω•́)っ✎⁾⁾!

    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,或者关注博主,在此感谢!

    万水千山总是情,打赏5毛买辣条行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主(っ•̀ω•́)っ✎⁾⁾!

    想问问题,打赏了卑微的博主,求求你备注一下的扣扣或者微信;这样我好联系你;(っ•̀ω•́)っ✎⁾⁾!

    支付宝
    微信
    本文版权归作者所有,欢迎转载,未经作者同意须保留此段声明,在文章页面明显位置给出原文连接
    如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    vue中使用AES.js和crypto.js加密
    vue项目中使用日期获取今日,昨日,上周,下周,上个月,下个月的数据
    vue项目中的路由守卫
    vue中携带token以及发送ajax
    vue项目中的字符串每隔4位一个空格
    vue中Echarts的使用-自选效果
    平衡树——Treap
    2021牛客寒假算法训练营3题解(9/10)
    2021牛客寒假算法训练营1题解(9/10)
    模板、知识点积累
  • 原文地址:https://www.cnblogs.com/IwishIcould/p/15220507.html
Copyright © 2011-2022 走看看