当前位置:   article > 正文

vue与element小案例、小技巧_vue结合element实例

vue结合element实例


上传图片与预览

效果图

uploadPreview1.png


uploadPreview2.png


uploadPreview3.png


html

<template>
    <div class="box">
        <el-popover width="200" placement="top" trigger="hover">
            <el-image v-if="avatarImg" width="200px" style="width: 100%; height: 130px; border-radius: 5px;"
                :src="avatarImg" :preview-src-list="[avatarImg]">
            </el-image>
            <div v-else
                style="width: 100%; height: 130px; line-height: 130px; text-align: center; background-color: #eeeeee; border-radius: 4px; cursor: pointer;"
                @click.prevent="handleUpload">
                暂无图片
            </div>
            <template #reference>
                <el-button class="coll" type="info" @click.prevent="handleUpload">上传图片</el-button>
            </template>
        </el-popover>
    </div>
</template>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

JavaScript

export default {
    name: 'Login',
    data() {
        return {
            avatarImg: ''
        }
    },
    methods: {
        async handleUpload() {
            try {
                const [fileHandle] = await window.showOpenFilePicker();
                const file = await fileHandle.getFile();

                const formData = new FormData();
                formData.append('file', file);

                this.avatarImg = URL.createObjectURL(file);

                // 上传到服务器
                // const response = await fetch('/upload', {
                //     method: 'POST',
                //     body: formData
                // });

                // console.log('response: ', response);
            } catch (error) {
                console.error('Error selecting file:', error);
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

style

.box {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;

    .login-btn-coll {
        .coll {
            width: 100%;
            height: 50px;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/661936
推荐阅读