uniapp实战 —— 分类导航【详解】

效果预览

组件封装

src\pages\index\components\CategoryPanel.vue

html 复制代码
<script setup lang="ts">
import type { CategoryItem } from '@/types/index'
defineProps<{
  list: CategoryItem[]
}>()
</script>

<template>
  <view class="category">
    <navigator
      class="category-item"
      hover-class="none"
      :url="item.url"
      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>

类型声明

src\types\index.d.ts

ts 复制代码
/** 首页-分类导航 */
export type CategoryItem = {
  /** 图标路径 */
  icon: string
  /** id */
  id: string
  /** 分类名称 */
  name: string
  // 导航地址
  url: string
}

相关接口

src\apis\index.ts

ts 复制代码
import { http } from '@/utils/http'
import type { CategoryItem } from '@/types/index'

/**
 * 首页-分类导航
 */
export const getHomeCategoryAPI = () => {
  return http<CategoryItem[]>({
    method: 'GET',
    url: '/home/category/mutli',
  })
}

实战使用

src\pages\index\index.vue

js 复制代码
import CategoryPanel from './components/CategoryPanel.vue'
html 复制代码
 <CategoryPanel :list="CategoryList" />
js 复制代码
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import type { CategoryItem } from '@/types/'
import { getHomeCategoryAPI } from '@/apis/index'
js 复制代码
let CategoryList = ref<CategoryItem[]>([])
const getCategoryInfo = async () => {
  let res = await getHomeCategoryAPI()
  CategoryList.value = res.result
}

onLoad(() => {
  getCategoryInfo()
})
相关推荐
酷炫码神1 小时前
Day 10|Pinia 状态管理:把散落在各组件的数据,收进一个全局仓库
android·开发语言·javascript
酷炫码神4 小时前
Day 8|Vue3 响应式:告别手动操作 DOM,让数据驱动界面
javascript·vue.js·ecmascript
码林鼠5 小时前
flutter状态管理入门教程-Riverpod
开发语言·javascript·flutter
互影派5 小时前
分享:Cocos 互动影游一套代码双端分发(Web H5 + Windows/Steam)实践
javascript
Amos_Web5 小时前
Markdonw语法
开发语言·前端·javascript
全栈项目管理程序猿6 小时前
ArcGIS JS 基础教程(34):体元系列 - 等值面 VoxelIsosurface
前端·javascript
SensorFlow6 小时前
离页埋点:sendBeacon 返回 true,为什么还不能算接收成功
javascript
兔子零10246 小时前
架构师最危险的能力:用正确的技术,做出错误的架构决策
前端·javascript·后端
Bug收容所6 小时前
Vue+js Day2
前端·javascript·vue.js
掘金酱7 小时前
带着作品来,坐下来聊|掘金 X-lab · Coffee Talk 邀请你来见一面
前端·javascript·后端