小程序学习(十七)之获取前台分类数据并渲染

复制代码
//CategoryPanel.vue
<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>

//index.vue
<style lang="scss">
page {
  background-color: "#f7f7f7";
}
</style>
复制代码
// services/home.ts
/**
 * 首页-前台分类-小程序
 */
export const getHomeCategoryAPI = () => {
  return http<CategoryItem[]>({
    method: 'GET',
    url: '/home/category/mutli',
  })
}

//index.vue
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}

onLoad(() => {
  getHomeBannerData()
  getHomeCategoryData()
})
复制代码
//home.d.ts
/** 首页-前台类目数据类型 */
export type CategoryItem = {
  /** 图标路径 */
  icon: string
  /** id */
  id: string
  /** 分类名称 */
  name: string
}

//index.vue
<template>
  <CustomNavbar />
  <XtxSwiper :list="bannerList" />  
  <CategoryPanel :list="categoryList" />
  <view class="index">index</view>
</template>

//CategoryPanel.vue
<script setup lang="ts">
import type { CategoryItem } from '@/types/home'

// 定义 props 接收数据
defineProps<{
  list: CategoryItem[]
}>()

</script>

<template>
  <view class="category">
    <navigator
      class="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>

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

觉得有帮助的话,打赏一下呗。。

需要商务合作(定制程序)的欢迎私信!!

相关推荐
CHU7290351 天前
随时随地学新知——线上网课教学小程序前端功能详解
前端·小程序
职豚求职小程序1 天前
京东26春招笔试题库宝藏小程序刷题yyds!
小程序
游戏开发爱好者81 天前
如何使用Instruments和Keymob进行Swift应用性能优化分析
开发语言·ios·性能优化·小程序·uni-app·iphone·swift
2501_916008891 天前
移动应用上架到应用商店的完整指南:原理与详细步骤
android·ios·小程序·https·uni-app·iphone·webview
云起SAAS1 天前
素材库微商品牌产品花店图文视频素材库抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·素材库微商品牌产品花店图文视频
源码ym7k资源1 天前
家政服务小程序维修保洁美容美甲上门预约派单技师saas全开源app+h5+小程序-源码ym7K
小程序
2601_952013761 天前
新麦同城到家预约上门服务系统小程序SAAS账号
小程序
源码ym7k资源1 天前
2024仿东郊微端家政预约上门小程序【亲测可用】家政管理系统抢单派单开源源码 -源码开发
小程序
黑客老李1 天前
记一次edu证书站小程序漏洞挖掘
小程序·黑客入门·渗透测试实战
ok_hahaha2 天前
java从头开始-苍穹外卖-day06-微信小程序开发-微信登录和商品浏览
java·微信·微信小程序·小程序