uni-app D4 实战(小兔鲜)

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

1.1 轮播图的静态结构(直接复制就好)

ts 复制代码
<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 首页-获取轮播图数据

相关推荐
@PHARAOH3 分钟前
WHAT - Vite 8(Rolldown 底层构建引擎升级)
前端·架构
阿Q的AI笔记6 分钟前
从ReAct到GRPO、从OpenHands到SWE-agent:Agent完整知识体系全拆解
前端·react.js·前端框架
计算机魔术师12 分钟前
从六个月路线图到JIT规划:代码评审不只是审代码
前端
迅猛龙办公室17 分钟前
Python实现PM空气质量提醒
服务器·前端·python
hhb_61826 分钟前
智联AI办公微服务技术全解析
人工智能·微服务·架构
智恒百亿34 分钟前
5090 八卡服务器踩坑实录:架构、多卡通信、PyTorch Distributed 一次说清楚
服务器·pytorch·架构
阿文和她的Key1 小时前
国产AI芯片涨价20%-50%:企业算力采购的四层成本架构重估
人工智能·架构
鬼手点金1 小时前
模型即 Agent:当模型本身成为产品
开发语言·前端·人工智能·python·机器学习
kiracrimson2 小时前
npm 入门:包管理器、依赖与 npx
前端·npm·node.js