VUE 笔记 ilovecoding 163 首页开发-首页商品数据的展示
内容来自 B站 ilovecoding 《最全最新Vue、Vuejs教程,从入门到精通》
1. 在 components/content 下增加目录 goods,该目录下增加组件 GoodsList 和 GoodsListItem
<template>
<div class="goods-item">
<img :src="goodsItem.show.img" alt=""/>
<div class="goods-info">
<p>{{goodsItem.title}}</p>
<span class="price">{{goodsItem.price}}</span>
<span class="collect">{{goodsItem.cfav}}</span>
</div>
</div>
</template>
<script>
export default {
name: 'GoodsListItem',
props: {
goodsItem: {
type: Object,
default() {
return {}
}
}
}
}
</script>
<style scoped>
.goods-item {
padding-bottom: 40px;
position: relative;
width: 48%;
}
.goods-item img {
width: 100%;
border-radius: 5px;
}
.goods-info {
font-size: 12px;
position: absolute;
bottom: 5px;
left: 0;
right: 0;
overflow: hidden;
text-align: center;
}
.goods-info p {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
margin-bottom: 3px;
}
.goods-info .price {
color: var(--color-high-text);
margin-right: 20px;
}
.goods-info .collect {
position: relative;
}
.goods-info .collect::before {
content: '';
position: absolute;
left: -15px;
top: -1px;
width: 14px;
height: 14px;
background: url("~assets/img/common/collect.svg") 0 0/14px 14px;
}
</style>
<template>
<div class="goods">
<goods-list-item v-for="(item, index) in goods" :key="index" :goods-item="item"/>
</div>
</template>
<script>
import GoodsListItem from './GoodsListItem'
export default {
name: 'GoodsList',
components: {
GoodsListItem
},
props: {
goods: {
type: Array,
default() {
return []
}
}
}
}
</script>
<style scoped>
.goods {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
padding: 2px;
}
</style>
2. 在 Home.vue 中使用 GoodList 组件
<template>
<div id="home">
<nav-bar class="home-nav">
<template v-slot:center>
<div>购物街</div>
</template>
</nav-bar>
<home-swiper :banners="banners"/>
<recommend-view :recommends="recommends"/>
<feature-view />
<tab-control class="tab-control" :titles="['流行', '新款', '精选']"/>
<goods-list :goods="goods['pop'].list"/>
</div>
</template>
<script>
import HomeSwiper from './childComps/HomeSwiper'
import RecommendView from './childComps/RecommendView'
import FeatureView from './childComps/FeatureView'
import NavBar from 'components/common/navbar/NavBar'
import TabControl from 'components/content/tabControl/TabControl'
import GoodsList from 'components/content/goods/GoodsList'
import { getHomeMultiData, getHomeGoods } from 'network/home'
export default {
name: 'Home',
components: {
HomeSwiper,
RecommendView,
FeatureView,
NavBar,
TabControl,
GoodsList
},
data() {
return {
banners: [],
recommends: [],
goods: {
'pop': {page: 0, list: []},
'new': {page: 0, list: []},
'sell': {page: 0, list: []}
}
}
},
methods: {
getHomeMultiData() {
getHomeMultiData().then(res => {
this.banners = res.data.banner.list
this.recommends = res.data.recommend.list
})
},
getHomeGoods(type) {
const page = this.goods[type].page + 1
getHomeGoods(type, page).then(res => {
this.goods[type].list.push(...res.data.list)
this.goods[type].page++
})
}
},
created() {
// 1. 请求多个数据
this.getHomeMultiData()
// 2. 请求商品数据
this.getHomeGoods('pop')
this.getHomeGoods('new')
this.getHomeGoods('sell')
}
}
</script>
<style>
#home {
padding-top: 44px;
}
.home-nav {
background-color: var(--color-tint);
color: #fff;
position: fixed;
left: 0;
right: 0;
top: 0;
z-index: 9;
}
.tab-control {
position: sticky;
top: 44px;
z-index: 9;
}
</style>
浙公网安备 33010602011771号