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()
})
相关推荐
2601_949868361 天前
Flutter for OpenHarmony 电子合同签署App实战 - 主入口实现
开发语言·javascript·flutter
m0_748229991 天前
Vue2 vs Vue3:核心差异全解析
前端·javascript·vue.js
xiaoxue..1 天前
React 手写实现的 KeepAlive 组件
前端·javascript·react.js·面试
摘星编程1 天前
在OpenHarmony上用React Native:自定义useHighlight关键词高亮
javascript·react native·react.js
2601_949613021 天前
flutter_for_openharmony家庭药箱管理app实战+用药知识详情实现
android·javascript·flutter
一起养小猫1 天前
Flutter for OpenHarmony 实战 表单处理与验证完整指南
android·开发语言·前端·javascript·flutter·harmonyos
xcs194051 天前
前端 项目构建问题 \node_modules\loader-runner\lib\loadLoader.js
开发语言·前端·javascript
我爱加班、、1 天前
new Map()+Array.from()整理elementPlus的级联器数据
linux·前端·javascript
Hx_Ma161 天前
Map集合的5种遍历方式
java·前端·javascript
css趣多多1 天前
render函数
前端·javascript·vue.js