在这篇文章中,我们将基于《黑马商城》项目,分析首页的实现。
涉及到调用uniapp组件库的轮播图组件,
图标
自建组件
首页效果如下:
分为三部分:第一部分是轮播图的实现
第二部分是导航区,涉及图标,文字等
第三部分是商品列表
1、轮播图
调用uniapp提供的组件滑块视图容器,代码如下:
<swiper indicator-dots circular>
<swiper-item v-for="item in swipers" :key="item.id">
<image :src="item.img"></image>
</swiper-item>
</swiper>
属性中,indicator-dots指显示面板指示点,circular指采用衔接滑动,滑动到末尾再往下滑可以回到开头,
没有为属性属性赋值,默认为true,即默认显示面板指示点和衔接滑动
在生命周期函数加载页面onLoad()时,获取轮播图数据
代码如下:
onLoad() {
this.getSwipers()
this.getHotGoods()
},
components: {"goods-list":goodsList},
methods: {
// 获取轮播图的数据
async getSwipers () {
const res = await this.$myRuquest({
url: '/api/getlunbo'
})
this.swipers = res.data.message
},
使用postman测试接口获取数据如下:
2、导航区域实现
导航区域html代码如下:
<!-- 导航区域 -->
<view class="nav">
<view class="nav_item" v-for="(item,index) in navs" :key="index" @click="navItemClick(item.path)">
<view :class="item.icon"></view>
<text>{
{item.title}}</text>
</view>
</view>
navs列表的数据在前端代码中存储,如下:
data() {
return {
swipers: [],
goods: [],
navs: [
{
icon: 'iconfont icon-ziyuan',
title: '黑马超市',
path: '/pages/goods/goods'
},
{
icon: 'iconfont icon-guanyuwomen',
title: '联系我们',
path: '/pages/contact/contact'
},