下面是一个简单的 Vue3 图片预览组件的示例代码:
<template>
<div>
<img :src="currentImage" alt="Preview" />
<button @click="prevImage">Prev</button>
<button @click="nextImage">Next</button>
</div>
</template>
<script>
export default {
name: 'ImagePreview',
data() {
return {
images: ['image1.jpg', 'image2.jpg', 'image3.jpg'],
currentIndex: 0
}
},
computed: {
currentImage() {
return this.images[this.currentIndex]
}
},
methods: {
prevImage() {
this.currentIndex = (this.currentIndex + this.images.length - 1) % this.images.length
},
nextImage() {
this.currentIndex = (this.currentIndex + 1) % this.images.length
}
}
}
</script>