当前位置:首页 > VUE

vue 实现商品分类

2026-02-20 07:09:42VUE

Vue 实现商品分类的方法

在 Vue 中实现商品分类功能,通常需要结合组件化开发、状态管理和 API 交互。以下是几种常见的实现方式。

使用动态组件渲染分类列表

通过 v-for 动态渲染分类列表,结合点击事件切换当前分类。

vue 实现商品分类

<template>
  <div>
    <ul class="category-list">
      <li 
        v-for="category in categories" 
        :key="category.id"
        @click="selectCategory(category)"
        :class="{ active: currentCategory.id === category.id }"
      >
        {{ category.name }}
      </li>
    </ul>
    <div class="product-list">
      <div v-for="product in currentProducts" :key="product.id">
        {{ product.name }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      categories: [],
      currentCategory: {},
      products: []
    };
  },
  computed: {
    currentProducts() {
      return this.products.filter(
        product => product.categoryId === this.currentCategory.id
      );
    }
  },
  methods: {
    selectCategory(category) {
      this.currentCategory = category;
    }
  },
  async created() {
    // 获取分类数据
    this.categories = await fetchCategories();
    // 获取商品数据
    this.products = await fetchProducts();
    // 设置默认选中第一个分类
    if (this.categories.length > 0) {
      this.currentCategory = this.categories[0];
    }
  }
};
</script>

使用 Vue Router 实现分类路由

通过路由参数动态加载不同分类的商品。

// router.js
{
  path: '/category/:id',
  name: 'Category',
  component: CategoryPage
}
<!-- CategoryPage.vue -->
<template>
  <div>
    <h2>{{ category.name }}</h2>
    <ProductList :products="products" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      category: {},
      products: []
    };
  },
  async created() {
    const categoryId = this.$route.params.id;
    this.category = await fetchCategory(categoryId);
    this.products = await fetchProductsByCategory(categoryId);
  },
  watch: {
    '$route.params.id'(newId) {
      this.loadCategoryData(newId);
    }
  },
  methods: {
    async loadCategoryData(categoryId) {
      this.category = await fetchCategory(categoryId);
      this.products = await fetchProductsByCategory(categoryId);
    }
  }
};
</script>

使用 Vuex 管理分类状态

对于大型应用,可以使用 Vuex 集中管理分类和商品数据。

vue 实现商品分类

// store.js
export default new Vuex.Store({
  state: {
    categories: [],
    currentCategoryId: null,
    products: []
  },
  mutations: {
    SET_CATEGORIES(state, categories) {
      state.categories = categories;
    },
    SET_CURRENT_CATEGORY(state, categoryId) {
      state.currentCategoryId = categoryId;
    },
    SET_PRODUCTS(state, products) {
      state.products = products;
    }
  },
  actions: {
    async fetchCategories({ commit }) {
      const categories = await api.getCategories();
      commit('SET_CATEGORIES', categories);
      if (categories.length > 0) {
        commit('SET_CURRENT_CATEGORY', categories[0].id);
      }
    },
    async fetchProducts({ commit }) {
      const products = await api.getProducts();
      commit('SET_PRODUCTS', products);
    },
    selectCategory({ commit }, categoryId) {
      commit('SET_CURRENT_CATEGORY', categoryId);
    }
  },
  getters: {
    currentProducts: state => {
      return state.products.filter(
        product => product.categoryId === state.currentCategoryId
      );
    }
  }
});

实现多级分类

对于需要多级分类的场景,可以使用递归组件。

<template>
  <div>
    <CategoryTree 
      :categories="topLevelCategories" 
      @select="selectCategory"
    />
  </div>
</template>

<script>
import CategoryTree from './CategoryTree.vue';

export default {
  components: { CategoryTree },
  data() {
    return {
      categories: []
    };
  },
  computed: {
    topLevelCategories() {
      return this.categories.filter(c => !c.parentId);
    }
  },
  async created() {
    this.categories = await fetchCategories();
  },
  methods: {
    selectCategory(category) {
      console.log('Selected category:', category);
    }
  }
};
</script>
<!-- CategoryTree.vue -->
<template>
  <ul>
    <li v-for="category in categories" :key="category.id">
      <div @click="$emit('select', category)">
        {{ category.name }}
      </div>
      <CategoryTree 
        v-if="hasChildren(category.id)"
        :categories="getChildren(category.id)"
        @select="$emit('select', $event)"
      />
    </li>
  </ul>
</template>

<script>
export default {
  name: 'CategoryTree',
  props: ['categories'],
  methods: {
    hasChildren(categoryId) {
      return this.categories.some(c => c.parentId === categoryId);
    },
    getChildren(categoryId) {
      return this.categories.filter(c => c.parentId === categoryId);
    }
  }
};
</script>

性能优化建议

对于大型商品分类系统,可以考虑以下优化措施:

  • 使用虚拟滚动处理大量分类项
  • 实现懒加载分类下的商品
  • 添加分类缓存机制
  • 使用防抖处理分类切换事件

这些方法可以根据实际项目需求组合使用,构建出高效的商品分类系统。

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

相关文章

vue表格重置怎么实现

vue表格重置怎么实现

重置表格数据的方法 在Vue中重置表格数据通常需要操作绑定的数据源,以下是几种常见实现方式: 直接重置数据源 // 假设表格绑定的是tableData this.tableData = [...t…

vue实现自定义登录

vue实现自定义登录

实现自定义登录的基本步骤 在Vue中实现自定义登录功能通常需要结合前端和后端技术。以下是一个基本的实现流程: 创建登录表单组件 使用Vue的单文件组件创建一个登录表单,包含用户名和密码输入框以及提…

vue实现全屏

vue实现全屏

Vue 实现全屏功能的方法 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 来实现。以下是几种常见的实现方式: 使用原生 Fullscreen API 通过调用 do…

vue 组件实现

vue 组件实现

Vue 组件实现方法 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template> <div class="example">{{…

实现vue table

实现vue table

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以快速渲染表格数据。在模板中定义表格结构,通过数据绑定动态生成行和列。 <template> <tabl…

vue实现动画

vue实现动画

Vue 实现动画的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接操作 CSS 或 JavaScript 动画。 使用 Vue 内置过渡系统 Vue 的 <…