uni-app D4 实战(小兔鲜)

142 阅读6分钟

1. 首页-封装通用轮播组件提高复用

image.png

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)

image.png

1.2.1 添加组件类型声明

image.png

image.png

1.3 轮播图的dots实现动态

image.png

image.png 结果:

image.png

image.png

image.png **本次练习重点知识点: ** image.png

1.4 首页-获取轮播图数据

1.4.1

image.png

image.png

1.4.2

image.png

image.png image.png

image.png

image.png

image.png

1.5 轮播图总结

image.png

2. 前台分类组件

2.1 前台分类组件的注册和在页面的导入使用

image.png 静态的组件代码:

<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>

image.png

2.2 获取前台分类数据

image.png

2.2.1 调用接口:

image.png

2.2.2 编写接口文件(首页接口文件)

service/home.ts

image.png

2.2.3 在页面中导入并使用API

image.png

2.3 首页-前台分类数据类型

image.png

image.png

image.png image.png

2.4 首页-前台分类数据渲染

父子组件通讯 动态渲染:

image.png

2.5 前台分类结果:

image.png

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的格式

image.png

3.3 导入到页面中

image.png

3.4 返回的后端API数据:

image.png

3.5 根据后端返回的数据格式编写数据类型文件

image.png

image.png

结果:

image.png

4. 猜你喜欢组件(需要封装成通用组件)

image.png

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 组件类型说明

image.png

4.3 要固定住自定义导航栏

4.3.1 将需要滚动的组件放入sroll view标签中

image.png

4.3.2 设置高度(使用flex布局 父:flex; 子:flex1)

image.png

4.4 获取猜你喜欢数据

image.png

4.4.1 封装API

image.png

4.4.2 调用API

实现当onMouted渲染完后,自动调用封装好的API来渲染

image.png 获取到的后端数据的结构:(不仅包含数据信息,还包含页码)

image.png

4.5 猜你喜欢数据类型和列表渲染

4.5.1 数据类型定义

image.png

image.png

image.png

4.5.2 将两个类型说明文件组合起来

image.png

4.5.3 保存数据并渲染

image.png

image.png

4.6 总结

image.png 结果:

image.png

4.7 猜你喜欢分页准备

image.png

4.7.1 给滚动容器加上一个触底事件

image.png

4.7.2 获取猜你喜欢组件实例

image.png

4.7.3 给组件实例定义类型

image.png

4.7.4 回到主页给组件的实例ref定义类型

image.png

4.7.5 在组件内部暴露一些属性和方法

getHomeGoodsGuessLikeData名字太长,为了方便使用,所以起了个更短的名称 image.png 补充!!!!!血的教训,不要写(),也不要写; 否则报错 以下为更正内容:

image.png

4.7.6 调用组件实例内部的方法

image.png

4.8 猜你喜欢-分页加载

image.png 接口的请求参数:

image.png

分页参数类型:

image.png

4.8.1 在API传入定义好的参数

image.png

4.8.2 使用分页参数(在调用的时候传递)

image.png

4.8.3 将单纯的赋值改为追加新数据到原数组,且给页码++

使用TS的工具函数“Required”

image.png

4.8.4 触底加载数据效果展示:

image.png

4.8.5 注意:数据是有限的,如果数据加载完毕,还在触底反弹的话很浪费性能(详情见4.9:猜你喜欢分页条件)

4.9 猜你喜欢分页条件

image.png

4.9.1 页码累加条件判断

image.png

4.9.2 使用三元表达式更改底部加载

image.png

4.9.3结果:

image.png

5. 首页-下拉刷新(常见/重点)

image.png

5.1 给滚动容器开启刷新(refresher-enabled)

5.2 给滚动容器刷新绑定事件

测试一下: image.png

image.png

5.3 数据加载完成后,主动关闭refresher-enabled的动画(使用refresher-triggered="true/false")

image.png

5.4 如果使用5.3就是按顺序执行,上一个执行结束后,下一个才发送请求,浪费时间。所以我们这里使用Pormise.all,所有一起发送请求,当所有执行完后才结束

image.png

6.首页刷新时-猜你喜欢组件的下拉刷新

6.1猜你喜欢组件和5.4中用到的三个组件的区别是:

猜你喜欢组件的数据是由组件内部发起请求并由内部维护的,而其他三个的数据是页面获取完再传递给子组件的

image.png

image.png

6.2 重置数据

image.png

6.3 暴露组件的数据

image.png

6.4 下拉刷新,请求组件数据

image.png

6.5 结果:

image.png

7. 骨架屏(页面加载之前的空白页面)

image.png

7.1在微信开发者助手中生成

image.png 点击确定 image.png

7.2 复制微信开发者工具生成的skeleton文件,复制到uniapp中,且要写入组件页面

image.png

7.3 骨架与加载页面互斥

image.png

7.4 绑定自定义函数

image.png

image.png