多彩照片墙 使用Vue脚手架Vue-c快速搭建项目 使用父组件向子组件传递图片的地址 在子组件中接收圈片地址,使用v-for指令遍历渲染显示圈片
时间: 2024-02-09 21:09:06 浏览: 157
好的,那么你可以按照以下步骤来实现多彩照片墙:
1. 使用Vue CLI创建一个新的Vue项目,安装必要的依赖,比如vue-router、axios等。
2. 在src/components目录下创建一个名为PhotoWall的组件,在该组件内部定义一个名为imageList的数组,用于存储图片地址。你可以在created钩子函数中通过axios等方式从后端获取图片地址。
3. 在PhotoWall组件中定义一个子组件PhotoItem,用于显示单张图片。该组件接收一个名为imageUrl的props属性,用于指定图片地址。在PhotoItem组件内部使用props属性渲染图片。
4. 在PhotoWall组件内部使用v-for指令遍历imageList数组,使用PhotoItem组件渲染每个图片。具体代码如下:
PhotoWall组件:
```
<template>
<div class="photo-wall">
<PhotoItem v-for="(imageUrl, index) in imageList" :key="index" :imageUrl="imageUrl" />
</div>
</template>
<script>
import PhotoItem from './PhotoItem.vue'
import axios from 'axios'
export default {
name: 'PhotoWall',
components: {
PhotoItem
},
data () {
return {
imageList: []
}
},
created () {
axios.get('/api/getImageList').then(res => {
this.imageList = res.data
}).catch(err => {
console.log(err)
})
}
}
</script>
<style scoped>
.photo-wall {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
</style>
```
PhotoItem组件:
```
<template>
<div class="photo-item">
<img :src="imageUrl" alt="">
</div>
</template>
<script>
export default {
name: 'PhotoItem',
props: {
imageUrl: {
type: String,
required: true
}
}
}
</script>
<style scoped>
.photo-item {
width: 30%;
margin-bottom: 15px;
}
.photo-item img {
width: 100%;
}
</style>
```
在上面的代码中,我们在PhotoWall组件内部使用v-for指令遍历imageList数组,使用PhotoItem组件渲染每个图片。在PhotoItem组件内部使用props属性渲染图片。
当然,你需要根据你的具体需求进行修改和完善,比如添加一些交互效果、优化性能等。
阅读全文