Vue3+ElementPlus封裝圖片空間組件的門面實例
什么是圖片空間?
圖片空間就是專門管理我們上傳圖片的地方。
就好比用戶管理一樣,我們對要上傳的圖片進行管理。
這樣做的好處有哪些?
我們把可能需要的圖片都上傳到圖片管理中。在其他需要圖片的地方(如:商品圖片等)可以直接調(diào)用圖片空間組件,從圖片空間中進行選擇。
圖片空間的樣式多種多樣,但是它們的入口都是一樣的,所以這里封裝了圖片空間的門面。
1.PictureSpaceFacade.vue 組件
<script setup>
const props=defineProps({
// 圖片數(shù)組或字符串(http://a.com/1.jpg,http://a.com/2.jpg 用逗號隔開)
modelValue: {
type: [Array,String,Object],
},
// 圖片寬度
pictureWidth: {
type:String,
default: '100px'
},
// 圖片高度
pictureHeight:{
type:String,
default: '100px'
}
})
const emits=defineEmits(['update:modelValue'])
import {ref, watch} from "vue";
// 圖片數(shù)組
const pictureList = ref([])
// 監(jiān)聽props.modelValue變化
watch(() => props.modelValue, (newValue) => {
if (newValue){
pictureList.value=Array.isArray(newValue) ? newValue : props.modelValue.split(",");
}else{
pictureList.value = []
}
},{deep:true,immediate:true})
// 預覽圖片開關
const previewVisible = ref(false)
// 預覽圖片url
const previewImageUrl = ref('')
// 點擊預覽圖片按鈕回調(diào)
function previewPicture(url) {
previewVisible.value = true
previewImageUrl.value = url
}
// 點擊刪除圖片按鈕回調(diào)
function deletePicture(url) {
pictureList.value = pictureList.value.filter(item => url !== item)
emits('update:modelValue', pictureList.value)
}
</script>
<template>
<div id="app">
<div class="container">
<div class="container-item" :style="{width:pictureWidth ,height:pictureHeight}" v-for="item in pictureList" :key="item">
<img class="img-item" :src="item" alt=""/>
<div class="overlay">
<span class="opt" @click="previewPicture(item)">預覽</span>
<span class="opt" @click="deletePicture(item)">刪除</span>
</div>
</div>
<div class="container-item add-container" :style="{width:pictureWidth ,height:pictureHeight}">
<slot>+</slot>
</div>
</div>
<!-- 圖片預覽-->
<el-dialog v-model="previewVisible" style="overflow: auto">
<div style="height: 100%; width: 100%; display: flex; justify-content: center; align-items: center;">
<img style="object-fit: cover; height: 100%; width: 100%;" :src="previewImageUrl" alt="Preview Image"/>
</div>
</el-dialog>
</div>
</template><style scoped lang="scss">
.container {
display: flex;
flex-wrap: wrap;
width: 100%;
}
.container-item {
height: 150px;
width: 150px;
margin: 3px;
border: 1px solid #cecaca;
border-radius: 5px;
overflow: hidden;
position: relative;
&:hover .overlay {
opacity: 1;
}
.img-item {
height: 100%;
width: 100%;
}
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
color: #fff;
font-size: 14px;
opacity: 0;
transition: opacity .5s;
.opt {
cursor: pointer;
margin: 8px;
}
}
}
.add-container {
border: 1px dashed #cecaca;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
&:hover {
border: 1px dashed blue;
}
}
</style>2.PictureSpaceFacade 組件使用
<script setup>
import {ref} from "vue";
import PictureSpaceFacade from "./components/PictureSpaceFacade.vue";
// 獲取隨機圖片
async function getPicture() {
const response = await fetch('https://dog.ceo/api/breeds/image/random')
const res = await response.json()
return res.message
}
// 圖片數(shù)組
const pictureList = ref([])
// 初始化圖片數(shù)組(準備測試數(shù)據(jù))
async function initPicture() {
for (let i = 0; i < 3; i++) {
pictureList.value.push(await getPicture())
}
}
// 圖片空間開關
const pictureSpaceVisible = ref(false)
// 打開圖片空間
function openPictureSpace() {
pictureSpaceVisible.value = true
}
// 關閉圖片空間(從圖片空間選擇圖片后對 pictureList進行填充)
async function closePictureSpace() {
for (let i = 0; i < 2; i++) {
pictureList.value.push(await getPicture())
}
pictureSpaceVisible.value = false
}
initPicture()
</script><template>
<el-row>
<el-col :span="8">
<PictureSpaceFacade v-model="pictureList" width="220px" picture-width="100px" picture-height="100px">
<div style="height: 100%;width: 100%;display: flex;justify-content: center;align-items: center;"
@click="openPictureSpace">+
</div>
</PictureSpaceFacade>
</el-col>
</el-row>
<!-- 圖片空間彈出框-->
<el-dialog v-model="pictureSpaceVisible" title="圖片空間" @close="closePictureSpace">
</el-dialog>
</template>
<style scoped lang="scss">
</style>
圖片寬度和高度就是圖片自身。
鼠標懸浮會出現(xiàn)操作按鈕(這里的文字可以自行修改為icon圖標)。

點擊圖片添加框,會彈出圖片空間組件供我們選擇。
我們通過選項綁定將pcitureList存入圖片空間,這樣在圖片空間中就能夠修改我們的圖片數(shù)組。

總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
使用vue-markdown實現(xiàn)markdown文件預覽
Vue-Markdown 是一個輕量級且高效的庫,用于在Vue.js應用程序中渲染Markdown文本,下面我們來看看如何使用vue-markdown實現(xiàn)markdown文件預覽效果吧2025-04-04
vue.js實現(xiàn)插入數(shù)值與表達式的方法分析
這篇文章主要介紹了vue.js實現(xiàn)插入數(shù)值與表達式的方法,結合實例形式分析了vue.js常見的3種插入數(shù)值實現(xiàn)方式,并總結了vue.js插值與表達式相關使用技巧,需要的朋友可以參考下2018-07-07

