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 首页-获取轮播图数据

相关推荐
学习指针路上的小学渣14 分钟前
JavaScript笔记
前端·javascript
取名不易14 分钟前
在 nuxtjs中通过fabric.js实现画图功能
前端
冰珊孤雪24 分钟前
Android Studio Panda革命性升级:内存诊断、构建标准化与AI调试全解析
android·前端
用户8061381665927 分钟前
避免滥用“事件总线”
前端
Xiaoke27 分钟前
我终于搞懂了 Event Loop(宏任务 / 微任务)
前端
@大迁世界29 分钟前
13.在 React 中应怎样正确更新 state?
前端·javascript·react.js·前端框架·ecmascript
苏琢玉40 分钟前
Go + Vue 打包成一个单二进制的后台系统,我做了个后台脚手架
vue.js·golang
终端鹿42 分钟前
Suspense 异步组件与懒加载实战
前端·vue.js
清风细雨_林木木44 分钟前
CSS 报错:css-semicolonexpected 解决方案
前端·css
ai生成式引擎优化技术44 分钟前
TSPR-WEB-LLM-HIC 架构方案可开发系统
架构