当前位置:首页 > VUE

用Vue实现商品分类

2026-01-22 03:50:21VUE

使用 Vue 实现商品分类

数据结构设计

商品分类通常采用树形结构,每个分类节点包含 idnamechildren 等字段。例如:

const categories = [
  {
    id: 1,
    name: "电子产品",
    children: [
      {
        id: 101,
        name: "手机",
        children: [
          { id: 1011, name: "智能手机" },
          { id: 1012, name: "功能手机" }
        ]
      }
    ]
  }
]

组件化实现

创建一个递归组件 CategoryTree.vue 来渲染分类树:

<template>
  <ul>
    <li v-for="category in categories" :key="category.id">
      {{ category.name }}
      <CategoryTree 
        v-if="category.children && category.children.length" 
        :categories="category.children"
      />
    </li>
  </ul>
</template>

<script>
export default {
  name: 'CategoryTree',
  props: {
    categories: {
      type: Array,
      required: true
    }
  }
}
</script>

状态管理

对于大型应用,建议使用 Vuex 管理分类数据:

// store/modules/categories.js
export default {
  state: {
    categories: []
  },
  mutations: {
    SET_CATEGORIES(state, categories) {
      state.categories = categories
    }
  },
  actions: {
    async fetchCategories({ commit }) {
      const res = await api.getCategories()
      commit('SET_CATEGORIES', res.data)
    }
  }
}

交互功能实现

添加展开/折叠功能:

<template>
  <ul>
    <li v-for="category in categories" :key="category.id">
      <span @click="toggleExpand(category)">
        {{ category.name }}
      </span>
      <CategoryTree 
        v-if="category.children && category.children.length && category.expanded" 
        :categories="category.children"
      />
    </li>
  </ul>
</template>

<script>
export default {
  methods: {
    toggleExpand(category) {
      this.$set(category, 'expanded', !category.expanded)
    }
  }
}
</script>

API 集成

创建 API 服务层:

// api/categories.js
import axios from 'axios'

export default {
  getCategories() {
    return axios.get('/api/categories')
  },
  addCategory(parentId, name) {
    return axios.post('/api/categories', { parentId, name })
  }
}

样式优化

使用 CSS 增强视觉效果:

ul {
  list-style: none;
  padding-left: 20px;
}

li {
  cursor: pointer;
  padding: 5px 0;
}

li > span:hover {
  color: #42b983;
  font-weight: bold;
}

完整示例

主组件调用示例:

<template>
  <div>
    <CategoryTree :categories="categories" />
    <button @click="refresh">刷新分类</button>
  </div>
</template>

<script>
import CategoryTree from './CategoryTree.vue'
import api from '../api/categories'

export default {
  components: { CategoryTree },
  data() {
    return {
      categories: []
    }
  },
  created() {
    this.refresh()
  },
  methods: {
    async refresh() {
      const res = await api.getCategories()
      this.categories = res.data
    }
  }
}
</script>

该实现包含了商品分类的核心功能,可根据实际需求扩展编辑、删除等操作。对于性能优化,可考虑虚拟滚动技术处理大型分类树。

用Vue实现商品分类

标签: 商品分类Vue
分享给朋友:

相关文章

Vue 实现登出

Vue 实现登出

Vue 实现登出的方法 清除用户状态 在Vue应用中,登出通常需要清除用户的登录状态。可以通过清除Vuex中的用户数据或本地存储的token来实现。 // 在Vuex的actions中 logout…

利用Vue实现

利用Vue实现

Vue 实现方法 在 Vue 中实现功能通常涉及组件化开发、状态管理和响应式数据绑定。以下是常见的实现步骤: 创建 Vue 组件 使用 Vue 的单文件组件(SFC)结构,包含模板、脚本和样式部分…

Vue实现路由监

Vue实现路由监

Vue 实现路由监听的方法 在 Vue 中可以通过多种方式实现路由监听,以下是常见的几种方法: 使用 watch 监听 $route 对象 通过 Vue 的 watch 功能可以监听 $route…

Vue实现word导入

Vue实现word导入

Vue实现Word导入的方法 在Vue项目中实现Word文档导入功能,通常需要借助第三方库或插件。以下是几种常见的实现方式: 使用docx-parser库 安装docx-parser库: npm…

Vue实现聊天软件

Vue实现聊天软件

Vue实现聊天软件的关键步骤 环境准备与项目初始化 使用Vue CLI或Vite创建Vue 3项目,安装必要依赖如vue-router、pinia(状态管理)、socket.io-client(实时通…

Vue中实现路由

Vue中实现路由

Vue 路由的实现方法 在 Vue 中实现路由通常使用 Vue Router 库。以下是具体的实现步骤: 安装 Vue Router 通过 npm 或 yarn 安装 Vue Router: n…