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()
})
相关推荐
.生产的驴15 分钟前
SpringBoot 消息队列RabbitMQ 消息确认机制确保消息发送成功和失败 生产者确认
java·javascript·spring boot·后端·rabbitmq·负载均衡·java-rabbitmq
书中自有妍如玉1 小时前
layui时间选择器选择周 日月季度年
前端·javascript·layui
Riesenzahn1 小时前
canvas生成图片有没有跨域问题?如果有如何解决?
前端·javascript
f8979070701 小时前
layui 可以使点击图片放大
前端·javascript·layui
小贵子的博客1 小时前
ElementUI 用span-method实现循环el-table组件的合并行功能
javascript·vue.js·elementui
明似水1 小时前
掌握 Flutter 中的 `Overlay` 和 `OverlayEntry`:弹窗管理的艺术
javascript·flutter
忘不了情1 小时前
左键选择v-html绑定的文本内容,松开鼠标后出现复制弹窗
前端·javascript·html
笃励2 小时前
Angular面试题四
前端·javascript·angular.js
前端专业写bug2 小时前
jspdf踩坑 htmltocanvas
java·前端·javascript
哈哈哈hhhhhh2 小时前
vue 入门一
前端·javascript·vue.js