欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3+ElementPlus封裝圖片空間組件的門面實例

 更新時間:2024年09月18日 10:26:46   作者:勿語&  
圖片空間是用于管理上傳圖片的工具,可以讓用戶方便地存儲、管理和調(diào)用圖片,提高工作效率,它通常具備多樣的樣式,但操作入口統(tǒng)一,便于使用,通過圖片空間組件,用戶能直接在其他模塊(例如商品圖片)中選擇所需圖片

什么是圖片空間?

圖片空間就是專門管理我們上傳圖片的地方。

就好比用戶管理一樣,我們對要上傳的圖片進行管理。

這樣做的好處有哪些?

我們把可能需要的圖片都上傳到圖片管理中。在其他需要圖片的地方(如:商品圖片等)可以直接調(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})

// 預(yù)覽圖片開關(guān)
const previewVisible = ref(false)
// 預(yù)覽圖片url
const previewImageUrl = ref('')
// 點擊預(yù)覽圖片按鈕回調(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)">預(yù)覽</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>

    <!--    圖片預(yù)覽-->
    <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ù)組(準(zhǔn)備測試數(shù)據(jù))
async function initPicture() {
  for (let i = 0; i < 3; i++) {
    pictureList.value.push(await getPicture())
  }
}

// 圖片空間開關(guān)
const pictureSpaceVisible = ref(false)

// 打開圖片空間
function openPictureSpace() {
  pictureSpaceVisible.value = true
}

// 關(guān)閉圖片空間(從圖片空間選擇圖片后對 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>

圖片寬度和高度就是圖片自身。

鼠標(biāo)懸浮會出現(xiàn)操作按鈕(這里的文字可以自行修改為icon圖標(biāo))。

點擊圖片添加框,會彈出圖片空間組件供我們選擇。

我們通過選項綁定將pcitureList存入圖片空間,這樣在圖片空間中就能夠修改我們的圖片數(shù)組。

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 在vue中使用echarts實現(xiàn)上浮與下鉆效果

    在vue中使用echarts實現(xiàn)上浮與下鉆效果

    這篇文章主要介紹了在vue中使用echarts實現(xiàn)上浮與下鉆效果,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-11-11
  • vue 的 Render 函數(shù)

    vue 的 Render 函數(shù)

    Vue 推薦在絕大多數(shù)情況下使用模板來創(chuàng)建你的 HTML。然而在一些場景中,你真的需要 JavaScript 的完全編程的能力。這時你可以用渲染函數(shù),它比模板更接近編譯器。下面就和小編一起來學(xué)習(xí)下面文章內(nèi)容吧
    2021-09-09
  • Vue 使用中的小技巧

    Vue 使用中的小技巧

    這篇文章主要介紹了Vue 使用中的小技巧,是小編日常開發(fā)的時候用到的小技巧,需要的朋友可以參考下
    2018-04-04
  • vue.js層疊輪播效果的實例代碼

    vue.js層疊輪播效果的實例代碼

    這篇文章主要介紹了vue.js層疊輪播效果,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2018-11-11
  • 使用vue-markdown實現(xiàn)markdown文件預(yù)覽

    使用vue-markdown實現(xiàn)markdown文件預(yù)覽

    Vue-Markdown 是一個輕量級且高效的庫,用于在Vue.js應(yīng)用程序中渲染Markdown文本,下面我們來看看如何使用vue-markdown實現(xiàn)markdown文件預(yù)覽效果吧
    2025-04-04
  • vue.js實現(xiàn)插入數(shù)值與表達(dá)式的方法分析

    vue.js實現(xiàn)插入數(shù)值與表達(dá)式的方法分析

    這篇文章主要介紹了vue.js實現(xiàn)插入數(shù)值與表達(dá)式的方法,結(jié)合實例形式分析了vue.js常見的3種插入數(shù)值實現(xiàn)方式,并總結(jié)了vue.js插值與表達(dá)式相關(guān)使用技巧,需要的朋友可以參考下
    2018-07-07
  • Vue2項目配置@指向src路徑方式

    Vue2項目配置@指向src路徑方式

    這篇文章主要介紹了Vue2項目配置@指向src路徑方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Vue3?實現(xiàn)一個自定義toast?小彈窗功能

    Vue3?實現(xiàn)一個自定義toast?小彈窗功能

    這篇文章主要介紹了Vue3?實現(xiàn)一個自定義toast?小彈窗,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-09-09
  • vue實現(xiàn)整屏滾動切換

    vue實現(xiàn)整屏滾動切換

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)整屏滾動切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • 加速vue組件渲染之性能優(yōu)化

    加速vue組件渲染之性能優(yōu)化

    這篇文章主要介紹了加速vue組件渲染之性能優(yōu)化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04

最新評論