当前位置:首页 > 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>

分类筛选功能

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

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
}

样式优化

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

.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>

性能优化

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

vue实现商城分类

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

完整示例代码

<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实现屏幕共享

vue实现屏幕共享

Vue 实现屏幕共享的方法 使用 WebRTC 和 getDisplayMedia API 通过 navigator.mediaDevices.getDisplayMedia 捕获屏幕内容,结合 We…

vue实现文章锚点定位

vue实现文章锚点定位

实现文章锚点定位的方法 在Vue中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在Vu…

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…

vue实现排序

vue实现排序

Vue 实现排序的方法 在 Vue 中实现排序可以通过多种方式完成,以下是一些常见的方法: 使用计算属性排序数组 计算属性非常适合对数据进行排序,因为它会在依赖的数据变化时自动更新。以下是一个示例:…

vue 实现回复

vue 实现回复

Vue 实现回复功能 在 Vue 中实现回复功能通常涉及表单提交、数据绑定和列表渲染。以下是实现步骤和代码示例: 数据绑定与表单 <template> <div>…

vue轮询实现

vue轮询实现

Vue 轮询实现方法 在 Vue 中实现轮询可以通过以下几种方式: 使用 setInterval data() { return { pollInterval: null } },…