当前位置:首页 > VUE

vue实现商城分类

2026-03-10 10:15:49VUE

Vue 实现商城分类功能

数据结构设计

商城分类通常采用树形结构,包含父分类和子分类。数据结构示例:

categories: [
  {
    id: 1,
    name: "电子产品",
    children: [
      { id: 101, name: "手机" },
      { id: 102, name: "电脑" }
    ]
  },
  {
    id: 2,
    name: "服装",
    children: [
      { id: 201, name: "男装" },
      { id: 202, name: "女装" }
    ]
  }
]

分类组件实现

使用递归组件展示多级分类:

<template>
  <div class="category">
    <ul>
      <li v-for="category in categories" :key="category.id">
        {{ category.name }}
        <category-list v-if="category.children" :categories="category.children"/>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'CategoryList',
  props: ['categories']
}
</script>

分类筛选功能

通过计算属性实现商品筛选:

vue实现商城分类

computed: {
  filteredProducts() {
    if (!this.selectedCategory) return this.products
    return this.products.filter(
      product => product.categoryId === this.selectedCategory
    )
  }
}

路由配置

为每个分类创建独立路由:

{
  path: '/category/:id',
  name: 'Category',
  component: () => import('./views/Category.vue'),
  props: true
}

样式优化

添加交互样式提升用户体验:

vue实现商城分类

.category li {
  padding: 8px 12px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.category li:hover {
  background-color: #f5f5f5;
}

.active-category {
  font-weight: bold;
  color: #42b983;
}

移动端适配

实现响应式侧边栏分类:

<template>
  <div class="mobile-categories">
    <button @click="showCategories = !showCategories">
      分类菜单
    </button>
    <div v-show="showCategories" class="categories-dropdown">
      <!-- 分类内容 -->
    </div>
  </div>
</template>

性能优化

对于大型分类数据,建议:

  • 使用虚拟滚动技术
  • 实现懒加载子分类
  • 添加分类缓存机制

完整示例代码

<template>
  <div class="shop-container">
    <div class="category-sidebar">
      <category-list 
        :categories="categories" 
        @select="selectCategory"
      />
    </div>
    <div class="product-list">
      <product-item 
        v-for="product in filteredProducts" 
        :key="product.id"
        :product="product"
      />
    </div>
  </div>
</template>

<script>
import CategoryList from './CategoryList.vue'
import ProductItem from './ProductItem.vue'

export default {
  components: { CategoryList, ProductItem },
  data() {
    return {
      selectedCategory: null,
      categories: [], // 从API获取
      products: []   // 从API获取
    }
  },
  methods: {
    selectCategory(categoryId) {
      this.selectedCategory = categoryId
    }
  }
}
</script>

标签: 商城vue
分享给朋友:

相关文章

vue实现多个tab

vue实现多个tab

Vue 实现多个 Tab 的方法 使用动态组件切换 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现多个…

用vue实现搜索查询

用vue实现搜索查询

实现搜索查询的基本思路 在Vue中实现搜索查询功能通常涉及以下几个核心部分:数据绑定、事件监听、过滤逻辑和UI反馈。以下是一个完整的实现方案。 数据绑定与输入框 创建一个Vue组件,包含一个输入框用…

vue 方法实现

vue 方法实现

在 Vue 中,方法的实现通常通过 methods 选项完成。以下是 Vue 方法实现的核心要点和示例: 基本方法定义 在 Vue 组件中定义方法时,需将函数声明放在 methods 对象内。这些方…

代码实现vue

代码实现vue

创建Vue项目 使用Vue CLI快速初始化项目,确保已安装Node.js和npm/yarn: npm install -g @vue/cli vue create my-vue-project c…

vue实现erp

vue实现erp

Vue实现ERP系统的关键步骤 技术选型与架构设计 采用Vue 3 + TypeScript构建前端,搭配Pinia状态管理,Element Plus或Ant Design Vue作为UI框架。后端可…

vue实现异步

vue实现异步

Vue 实现异步操作的常见方法 Vue 提供了多种方式来处理异步操作,包括数据获取、事件处理等场景。 使用 async/await 在 Vue 方法中可以直接使用 async/await 语法处理异…