1. 首页-封装通用轮播组件提高复用
1.1 轮播图的静态结构(直接复制就好)
<script setup lang="ts">
import { ref } from 'vue'
const activeIndex = ref(0)
</script>
<template>
<view class="carousel">
<swiper :circular="true" :autoplay="false" :interval="3000">
<swiper-item>
<navigator url="/pages/index/index" hover-class="none" class="navigator">
<image
mode="aspectFill"
class="image"
src="https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/uploads/slider_1.jpg"
></image>
</navigator>
</swiper-item>
<swiper-item>
<navigator url="/pages/index/index" hover-class="none" class="navigator">
<image
mode="aspectFill"
class="image"
src="https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/uploads/slider_2.jpg"
></image>
</navigator>
</swiper-item>
<swiper-item>
<navigator url="/pages/index/index" hover-class="none" class="navigator">
<image
mode="aspectFill"
class="image"
src="https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/uploads/slider_3.jpg"
></image>
</navigator>
</swiper-item>
</swiper>
<!-- 指示点 -->
<view class="indicator">
<text
v-for="(item, index) in 3"
:key="item"
class="dot"
:class="{ active: index === activeIndex }"
></text>
</view>
</view>
</template>
<style lang="scss">
/* 轮播图 */
.carousel {
height: 280rpx;
position: relative;
overflow: hidden;
transform: translateY(0);
background-color: #efefef;
.indicator {
position: absolute;
left: 0;
right: 0;
bottom: 16rpx;
display: flex;
justify-content: center;
.dot {
width: 30rpx;
height: 6rpx;
margin: 0 8rpx;
border-radius: 6rpx;
background-color: rgba(255, 255, 255, 0.4);
}
.active {
background-color: #fff;
}
}
.navigator,
.image {
width: 100%;
height: 100%;
}
}
</style>
1.2 修改一下组件引入规则(pages.json)
1.2.1 添加组件类型声明
1.3 轮播图的dots实现动态
结果:
**本次练习重点知识点:
**
1.4 首页-获取轮播图数据
1.4.1
1.4.2
1.5 轮播图总结
2. 前台分类组件
2.1 前台分类组件的注册和在页面的导入使用
静态的组件代码:
<script setup lang="ts">
//
</script>
<template>
<view class="category">
<navigator
class="category-item"
hover-class="none"
url="/pages/index/index"
v-for="item in 10"
:key="item"
>
<image
class="icon"
src="https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/nav_icon_1.png"
></image>
<text class="text">居家</text>
</navigator>
</view>
</template>
<style lang="scss">
/* 前台类目 */
.category {
margin: 20rpx 0 0;
padding: 10rpx 0;
display: flex;
flex-wrap: wrap;
min-height: 328rpx;
.category-item {
width: 150rpx;
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
box-sizing: border-box;
.icon {
width: 100rpx;
height: 100rpx;
}
.text {
font-size: 26rpx;
color: #666;
}
}
}
</style>
2.2 获取前台分类数据
2.2.1 调用接口:
2.2.2 编写接口文件(首页接口文件)
service/home.ts
2.2.3 在页面中导入并使用API
2.3 首页-前台分类数据类型
2.4 首页-前台分类数据渲染
父子组件通讯 动态渲染:
2.5 前台分类结果:
3. 热门推荐组件
注意:因为只有首页会用到这个组件,所以直接写在组件里就好了
3.1 组件静态结构
<script setup lang="ts">
//
</script>
<template>
<!-- 推荐专区 -->
<view class="panel hot">
<view class="item" v-for="item in 4" :key="item">
<view class="title">
<text class="title-text">特惠推荐</text>
<text class="title-desc">精选全攻略</text>
</view>
<navigator hover-class="none" url="/pages/hot/hot" class="cards">
<image
class="image"
mode="aspectFit"
src="https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/uploads/goods_small_1.jpg"
></image>
<image
class="image"
mode="aspectFit"
src="https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/uploads/goods_small_2.jpg"
></image>
</navigator>
</view>
</view>
</template>
<style lang="scss">
/* 热门推荐 */
.hot {
display: flex;
flex-wrap: wrap;
min-height: 508rpx;
margin: 20rpx 20rpx 0;
border-radius: 10rpx;
background-color: #fff;
.title {
display: flex;
align-items: center;
padding: 24rpx 24rpx 0;
font-size: 32rpx;
color: #262626;
position: relative;
.title-desc {
font-size: 24rpx;
color: #7f7f7f;
margin-left: 18rpx;
}
}
.item {
display: flex;
flex-direction: column;
width: 50%;
height: 254rpx;
border-right: 1rpx solid #eee;
border-top: 1rpx solid #eee;
.title {
justify-content: start;
}
&:nth-child(2n) {
border-right: 0 none;
}
&:nth-child(-n + 2) {
border-top: 0 none;
}
.image {
width: 150rpx;
height: 150rpx;
}
}
.cards {
flex: 1;
padding: 15rpx 20rpx;
display: flex;
justify-content: space-between;
align-items: center;
}
}
</style>
3.2 请求API的格式
3.3 导入到页面中
3.4 返回的后端API数据:
3.5 根据后端返回的数据格式编写数据类型文件
结果:
4. 猜你喜欢组件(需要封装成通用组件)
4.1 组件静态结构
<script setup lang="ts">
//
</script>
<template>
<!-- 猜你喜欢 -->
<view class="caption">
<text class="text">猜你喜欢</text>
</view>
<view class="guess">
<navigator
class="guess-item"
v-for="item in 10"
:key="item"
:url="`/pages/goods/goods?id=4007498`"
>
<image
class="image"
mode="aspectFill"
src="https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/uploads/goods_big_1.jpg"
></image>
<view class="name"> 德国THORE男表 超薄手表男士休闲简约夜光石英防水直径40毫米 </view>
<view class="price">
<text class="small">¥</text>
<text>899.00</text>
</view>
</navigator>
</view>
<view class="loading-text"> 正在加载... </view>
</template>
<style lang="scss">
:host {
display: block;
}
/* 分类标题 */
.caption {
display: flex;
justify-content: center;
line-height: 1;
padding: 36rpx 0 40rpx;
font-size: 32rpx;
color: #262626;
.text {
display: flex;
justify-content: center;
align-items: center;
padding: 0 28rpx 0 30rpx;
&::before,
&::after {
content: '';
width: 20rpx;
height: 20rpx;
background-image: url(@/static/images/bubble.png);
background-size: contain;
margin: 0 10rpx;
}
}
}
/* 猜你喜欢 */
.guess {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
padding: 0 20rpx;
.guess-item {
width: 345rpx;
padding: 24rpx 20rpx 20rpx;
margin-bottom: 20rpx;
border-radius: 10rpx;
overflow: hidden;
background-color: #fff;
}
.image {
width: 304rpx;
height: 304rpx;
}
.name {
height: 75rpx;
margin: 10rpx 0;
font-size: 26rpx;
color: #262626;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.price {
line-height: 1;
padding-top: 4rpx;
color: #cf4444;
font-size: 26rpx;
}
.small {
font-size: 80%;
}
}
// 加载提示文字
.loading-text {
text-align: center;
font-size: 28rpx;
color: #666;
padding: 20rpx 0;
}
</style>
4.2 组件类型说明
4.3 要固定住自定义导航栏
4.3.1 将需要滚动的组件放入sroll view标签中
4.3.2 设置高度(使用flex布局 父:flex; 子:flex1)
4.4 获取猜你喜欢数据
4.4.1 封装API
4.4.2 调用API
实现当onMouted渲染完后,自动调用封装好的API来渲染
获取到的后端数据的结构:(不仅包含数据信息,还包含页码)
4.5 猜你喜欢数据类型和列表渲染
4.5.1 数据类型定义
4.5.2 将两个类型说明文件组合起来
4.5.3 保存数据并渲染
4.6 总结
结果:
4.7 猜你喜欢分页准备
4.7.1 给滚动容器加上一个触底事件
4.7.2 获取猜你喜欢组件实例
4.7.3 给组件实例定义类型
4.7.4 回到主页给组件的实例ref定义类型
4.7.5 在组件内部暴露一些属性和方法
getHomeGoodsGuessLikeData名字太长,为了方便使用,所以起了个更短的名称
补充!!!!!血的教训,不要写(),也不要写; 否则报错
以下为更正内容:
4.7.6 调用组件实例内部的方法
4.8 猜你喜欢-分页加载
接口的请求参数:
分页参数类型:
4.8.1 在API传入定义好的参数
4.8.2 使用分页参数(在调用的时候传递)
4.8.3 将单纯的赋值改为追加新数据到原数组,且给页码++
使用TS的工具函数“Required”
4.8.4 触底加载数据效果展示:
4.8.5 注意:数据是有限的,如果数据加载完毕,还在触底反弹的话很浪费性能(详情见4.9:猜你喜欢分页条件)
4.9 猜你喜欢分页条件
4.9.1 页码累加条件判断
4.9.2 使用三元表达式更改底部加载
4.9.3结果:
5. 首页-下拉刷新(常见/重点)
5.1 给滚动容器开启刷新(refresher-enabled)
5.2 给滚动容器刷新绑定事件
测试一下:
5.3 数据加载完成后,主动关闭refresher-enabled的动画(使用refresher-triggered="true/false")
5.4 如果使用5.3就是按顺序执行,上一个执行结束后,下一个才发送请求,浪费时间。所以我们这里使用Pormise.all,所有一起发送请求,当所有执行完后才结束
6.首页刷新时-猜你喜欢组件的下拉刷新
6.1猜你喜欢组件和5.4中用到的三个组件的区别是:
猜你喜欢组件的数据是由组件内部发起请求并由内部维护的,而其他三个的数据是页面获取完再传递给子组件的
6.2 重置数据
6.3 暴露组件的数据
6.4 下拉刷新,请求组件数据
6.5 结果:
7. 骨架屏(页面加载之前的空白页面)
7.1在微信开发者助手中生成
点击确定