基于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>
<!-- 這個是要點(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中實(shí)現(xiàn)路由跳轉(zhuǎn)的三種方式(超詳細(xì)整理)
這篇文章給大家詳細(xì)的整理了Vue中實(shí)現(xiàn)路由跳轉(zhuǎn)的三種方式,使用vue-router,聲明式-router-link,編程式這三種方法,分別有詳細(xì)的代碼示例,需要的朋友可以參考下2023-09-09
詳解如何從零開始搭建Express+Vue開發(fā)環(huán)境
這篇文章主要介紹了詳解如何從零開始搭建Express+Vue開發(fā)環(huán)境,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
vue項(xiàng)目設(shè)置打包后的靜態(tài)文件訪問路徑
這篇文章主要介紹了vue項(xiàng)目設(shè)置打包后的靜態(tài)文件訪問路徑,vue項(xiàng)目的最終項(xiàng)目文件需要經(jīng)過打包輸出,靜態(tài)文件的訪問路徑需要在vue.config.js文件中設(shè)置,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02
使用vue-cli webpack 快速搭建項(xiàng)目的代碼
這篇文章主要介紹了vue-cli webpack 快速搭建項(xiàng)目的教程詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-11-11
Vue.js組件props數(shù)據(jù)驗(yàn)證實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Vue.js組件props數(shù)據(jù)驗(yàn)證的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10

