当前位置: 首页 > news >正文

uni-appDay02

1.首页-通用轮播组件

轮播图组件需要再首页和分类页使用,封装成通用组件

  1. 准备组件
  2. 自动导入组件
<script setup lang="ts">
import XtxSwiper from '@/components/XtxSwiper.vue'
import CustomNavbar from './components/CustomNavbar.vue'
</script><template><CustomNavbarom /><XtxSwiper /><view class="index">index</view>
</template><style lang="scss">
//
</style>
  1. 添加组件类型声明
import XtxSwiper from './XtxSwiper.vue'
declare module '@vue/runtime-core' {export interface GlobalComponents {// 确认类型,全局定义XtxSwiper: typeof XtxSwiper}
}
2.轮播图-指示点

@change=“onChange”

<script setup lang="ts">
import { ref } from 'vue'const activeIndex = ref(0)
// 当swiper下标变化时触发
const onChange: UniHelper.SwiperOnChange = (ev) => {// 非空断言用!. 主观上排除掉空值情况activeIndex.value = ev.detail!.current
}
</script>
3.轮播图-获取轮播图数据
  1. 封装获取轮播图数据API
import { http } from '@/utils/http'export const getHomeBannerAPI = (distributionSite = 1) => {return http({method: 'GET',url: '/home/banner',data: {distributionSite,},})
}
  1. 页面初始化API
<script setup lang="ts">
import XtxSwiper from '@/components/XtxSwiper.vue'
import { getHomeBannerAPI } from '@/services/home'
import CustomNavbar from './components/CustomNavbar.vue'
import { onLoad } from '@dcloudio/uni-app'const bannerList = ref([])
const getHomeBannerData = async () => {const res = await getHomeBannerAPI()bannerList.value = res.result
}onLoad(() => {getHomeBannerData()
})
</script><template><CustomNavbarom /><XtxSwiper /><view class="index">index</view>
</template><style lang="scss">
//
</style>
4.首页-轮播图数据类型并渲染
  1. 定义轮播图数据类型
/** 首页-广告区域数据类型 */
export type BannerItem = {/** 跳转链接 */hrefUrl: string/** id */id: string/** 图片链接 */imgUrl: string/** 跳转类型 */type: number
}
  1. 指定类型并传值给子组件
import { http } from '@/utils/http'
import { BannerItem } from '@/types/home'export const getHomeBannerAPI = (distributionSite = 1) => {return http<BannerItem[]>({method: 'GET',url: '/home/banner',data: {distributionSite,},})
}<script setup lang="ts">
import { BannerItem } from '@/types/home'
import { ref } from 'vue'const activeIndex = ref(0)
// 当swiper下标变化时触发
const onChange: UniHelper.SwiperOnChange = (ev) => {// 非空断言用!. 主观上排除掉空值情况activeIndex.value = ev.detail!.current
}
defineProps<{list: BannerItem[]
}>()
</script>
  1. 渲染数据
<template><view class="carousel"><swiper :circular="true" :autoplay="false" :interval="3000" @change="onChange"><swiper-item v-for="item in list" :key="item.id"><navigator url="/pages/index/index" hover-class="none" class="navigator"><image mode="aspectFill" class="image" :src="item.imgUrl"></image></navigator></swiper-item></swiper><!-- 指示点 --><view class="indicator"><textv-for="(item, index) in list":key="item.id"class="dot":class="{ active: index === activeIndex }"></text></view></view>
</template>
5.前台分类-组件封装
  1. 准备组件(只有首页使用)
  2. 导入并使用组件
  3. 设置首页底色为#F7F7F7
// 导入
<script setup lang="ts">
import CategoryPanel from './components/CategoryPanel.vue'
</script><template><!-- 分类面板 --><CategoryPanel /><view class="index">index</view>
</template><style lang="scss">
page {background-color: #f7f7f7;
}
</style>
6.前台分类数据
  1. 封装获取前台分类数据API
export const getHomeCategoryAPI = () => {return http({method: 'GET',url: '/home/category/mutli',})
}
  1. 页面初始化调用API
<script setup lang="ts">
import XtxSwiper from '@/components/XtxSwiper.vue'
import { getHomeBannerAPI, getHomeCategoryAPI } from '@/services/home'
import CustomNavbar from './components/CustomNavbar.vue'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import type { BannerItem } from '@/types/home'
import CategoryPanel from './components/CategoryPanel.vue'// 获取轮播图数据
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {const res = await getHomeBannerAPI()bannerList.value = res.result
}const getHomeCategoryData = async () => {const res = await getHomeCategoryAPI()
}
// 页面加载
onLoad(() => {getHomeBannerData()getHomeCategoryData()
})
</script>
7.前台分类数据类型并渲染
  1. 定义前台分类数据类型
export const getHomeCategoryAPI = () => {return http<CategoryItem[]>({method: 'GET',url: '/home/category/mutli',})
}
  1. 指定类型并传值给子组件
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {const res = await getHomeCategoryAPI()categoryList.value = res.result
}
  1. 渲染前台分类数据
<script setup lang="ts">
import { CategoryItem } from '@/types/home'defineProps<{list: CategoryItem[]
}>()
</script><template><view class="category"><navigatorclass="category-item"hover-class="none"url="/pages/index/index"v-for="item in list":key="item.id"><image class="icon" :src="item.icon"></image><text class="text">{{ item.name }}</text></navigator></view>
</template>
8.首页-热门推荐
  1. 准备组件(只有首页使用)
  2. 导入并使用组件
  3. 封装获取热门推荐数据API
export const getHomeHotAPI = () => {return http({method: 'GET',url: '/home/hot/mutli',})
}
  1. 定义热门推荐数据类型并指定
/** 首页-热门推荐数据类型 */
export type HotItem = {/** 说明 */alt: string/** id */id: string/** 图片集合[ 图片路径 ] */pictures: string[]/** 跳转地址 */target: string/** 标题 */title: string/** 推荐类型 */type: string
}export const getHomeHotAPI = () => {return http<HotItem[]>({method: 'GET',url: '/home/hot/mutli',})
}
  1. 传递给子组件并渲染
// 获取热门推荐数据
const hotList = ref<HotItem[]>([])
const getHomeHotData = async () => {const res = await getHomeHotAPI()hotList.value = res.result
}<script setup lang="ts">
import { CategoryItem } from '@/types/home'defineProps<{list: CategoryItem[]
}>()
</script><template><view class="category"><navigatorclass="category-item"hover-class="none"url="/pages/index/index"v-for="item in list":key="item.id"><image class="icon" :src="item.icon"></image><text class="text">{{ item.name }}</text></navigator></view>
</template>
9.猜你喜欢-组件封装
  1. 准备组件(通用组件)
  2. 定义组件类型
import XtxSwiper from '../components/XtxSwiper.vue'
import XtxGuess from '../components/XtxGuess.vue'
declare module '@vue/runtime-core' {export interface GlobalComponents {// 确认类型,全局定义XtxSwiper: typeof XtxSwiperXtxGuess: typeof XtxGuess}
}
  1. 准备scroll-view滚动容器
<template><!-- 自定义导航栏 --><CustomNavbarom /><scroll-view class="scroll-view" scroll-y><!-- 自定义轮播图 --><XtxSwiper :list="bannerList" /><!-- 分类面板 --><CategoryPanel :list="categoryList" /><!-- 热门推荐 --><HotPanel :list="hotList" /><!-- 猜你喜欢 --><XtxGuess /></scroll-view>
</template>
  1. 设置page和scroll-view样式
<style lang="scss">
page {background-color: #f7f7f7;height: 100%;display: flex;flex-direction: column;
}
.scroll-view {flex: 1;
}
</style>
10.获取猜你喜欢数据
  1. 封装获取猜你喜欢数据API
http://www.lryc.cn/news/598080.html

相关文章:

  • uniapp中flex布局gap属性兼容处理
  • LockPatternUtils中比较重要的方法
  • day46.通道注意力
  • 【SpringAI实战】FunctionCalling实现企业级自定义智能客服
  • Quarkus利用 MicroProfile 实现微服务特性
  • 基于深度学习的图像分类:使用EfficientNet实现高效分类
  • 期货交易系统:市场生态中的功能映射与价值逻辑
  • uni-app小程序云效持续集成
  • Etcd原理基础学习
  • Java 垃圾回收器之CMS GC问题分析与解决
  • 二分查找----5.寻找旋转排序数组中的最小值
  • fabric搭建基础的测试网络
  • ARM 学习笔记(四)
  • 若依框架在 IDEA 中运行的前置软件环境配置指南
  • AI开放课堂:钉钉MCP开发实战
  • 4种灵活的方法从POCO手机中删除联系人
  • 移动管家手机控车便捷性如何
  • 数据库集群环境漏洞修复
  • uniapp写app做测试手机通知栏展示内容
  • AI结对编程:分布式团队的集体记忆外脑
  • TechGPT3部署
  • 初识opencv03——图像预处理2
  • 中国西北典型绿洲区土壤水分特征(2018-2019年)
  • 前端面试专栏-前沿技术:30.跨端开发技术(React Native、Flutter)
  • LeetCode 1695.删除子数组的最大得分:滑动窗口(哈希表)
  • 智慧工厂网络升级:新型 SD-WAN 技术架构与应用解析
  • 【Git知识】Git 常用知识集合之基础--分支系统与 Tag 标签机制
  • Leetcode 07 java
  • CodeBuddy IDE发布:编程新时代的颠覆者?
  • Golang实现 - 实现只有表头的 Excel 模板,并在指定列添加了下拉框功能。生成的 Excel 文件在打开时,指定列的单元格会显示下拉选项