基于Vue3封裝實(shí)現(xiàn)圖片查看器
需求
點(diǎn)擊圖片放大
可關(guān)閉放大的 圖片
下載
cnpm in viewerjs
狀態(tài)管理+方法
stores/imgSeeStore.js
import { defineStore } from 'pinia'
export const imgSeeStore = defineStore('imgSeeStore', {
state: () => ({
showImgSee: false,
ImgUrl: '',
}),
getters: {
},
actions: {
openImgShow(url) {
this.ImgUrl = url
this.showImgSee = true
},
resetSeeImg() {
this.ImgUrl = ''
this.showImgSee = false
}
}
})
封裝的組件
<template>
<img ref="el" :src="ImgUrl" />
</template>
<script setup>
import "viewerjs/dist/viewer.css";
import Viewer from "viewerjs";
import { nextTick, onMounted, ref } from "vue";
import { storeToRefs } from "pinia";
import { globalStateStore } from "src/stores/globalState";
const useGlobalStateStore = globalStateStore(),
{ ImgUrl } = storeToRefs(useGlobalStateStore),
{ resetSeeImg } = useGlobalStateStore;
const el = ref();
onMounted(async () => {
await nextTick();
// 圖片查看器關(guān)閉事件
el.value.addEventListener("hide", () => resetSeeImg());
new Viewer(el.value, {
navbar: false,
title: false,
}).show();
});
</script>
使用
TestVue.vue
<template>
<!-- 這個(gè)是要點(diǎn)擊查看的圖片 -->
<img
style="max-width: 200px"
:src="img"
@click="openImgShow(img)"
/>
<img-see v-if="showImgSee" />
</template>
<script setup>
import { ref} from "vue";
import { storeToRefs } from "pinia";
import ImgSee from "src/components/ImgSee.vue";
import { imgSeeStore} from "src/stores/imgSeeStore";
const img = ref('/public/test.jpg')
const useImgSeeStore= imgSeeStore(),
{ showImgSee } = storeToRefs(useImgSeeStore),
{ openImgShow } = useImgSeeStore;
</script>
到此這篇關(guān)于基于Vue3封裝實(shí)現(xiàn)圖片查看器的文章就介紹到這了,更多相關(guān)Vue3圖片查看器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue狀態(tài)管理庫(kù)Vuex的入門(mén)使用教程
Vuex是一個(gè)專(zhuān)門(mén)為Vue.js應(yīng)用程序開(kāi)發(fā)的狀態(tài)管理庫(kù)。它采用了一個(gè)集中式的架構(gòu),將應(yīng)用程序的所有組件的狀態(tài)存儲(chǔ)在一個(gè)單獨(dú)的地方。這使得狀態(tài)的管理和維護(hù)變得更加容易2023-03-03
vue 輸入框輸入任意內(nèi)容返回?cái)?shù)字的實(shí)現(xiàn)
本文主要介紹了vue 輸入框輸入任意內(nèi)容返回?cái)?shù)字的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
vue圓環(huán)百分比進(jìn)度條組件功能的實(shí)現(xiàn)
在一些頁(yè)面設(shè)置進(jìn)度條效果給人一種很好的體驗(yàn)效果,今天小編教大家vue圓環(huán)百分比進(jìn)度條組件功能的實(shí)現(xiàn)代碼,代碼超級(jí)簡(jiǎn)單啊,感興趣的朋友快來(lái)看下吧2021-05-05
基于Vue2實(shí)現(xiàn)語(yǔ)音報(bào)警功能的示例代碼
Vue2 實(shí)現(xiàn)消息語(yǔ)音報(bào)警功能 下面是一個(gè)完整的Vue2消息語(yǔ)音報(bào)警實(shí)現(xiàn)方案,包含多種語(yǔ)音播報(bào)方式和自定義配置。 1. 安裝依賴 2. 語(yǔ)音報(bào)警組件 核心語(yǔ)音服務(wù)類(lèi) Vue2語(yǔ)音報(bào)警組件 3. 全局事2025-11-11
vue靜態(tài)配置文件不進(jìn)行編譯的處理過(guò)程(在public中引入js)
這篇文章主要介紹了vue靜態(tài)配置文件不進(jìn)行編譯的處理過(guò)程(在public中引入js),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
vue3中ace-editor的簡(jiǎn)單使用方法實(shí)例
這篇文章主要給大家介紹了關(guān)于vue3中ace-editor簡(jiǎn)單使用的相關(guān)資料,ace-editor是一種代碼編輯器,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
Vue實(shí)現(xiàn)淘寶購(gòu)物車(chē)三級(jí)選中功能詳解
這篇文章主要介紹了通過(guò)Vue實(shí)現(xiàn)淘寶購(gòu)物車(chē)中三級(jí)選中的功能,文中的實(shí)現(xiàn)過(guò)程講解詳細(xì),對(duì)我們學(xué)習(xí)Vue有一定的幫助,感興趣的可以了解一下2022-01-01

