当前位置:首页 > VUE

vue 实现商品分类

2026-02-20 07:09:42VUE

Vue 实现商品分类的方法

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

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

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

<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 集中管理分类和商品数据。

// 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筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <div…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…

vue 实现modal

vue 实现modal

Vue 实现 Modal 的方法 使用 Vue 原生组件 通过 Vue 的组件系统可以轻松实现 Modal。创建一个可复用的 Modal 组件,通过 props 控制显示与隐藏。 <templ…

vue实现答辩

vue实现答辩

Vue 实现答辩系统的关键步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目,安装 Vue Router 管理路由,Pinia/Vuex 进行状态管理。配置 ESLint 和 Pre…

vue scrolltop 实现

vue scrolltop 实现

实现 Vue 中的 scrollTop 在 Vue 中实现滚动到顶部功能可以通过多种方式完成,包括使用原生 JavaScript、Vue 指令或第三方库。以下是几种常见的实现方法: 使用原生 Jav…

vue实现mvvm

vue实现mvvm

Vue 实现 MVVM 模式 MVVM(Model-View-ViewModel)是一种前端架构模式,Vue 是其典型实现之一。MVVM 的核心是通过数据绑定和响应式系统实现视图与数据的自动同步。…