当前位置:首页 > VUE

vue 实现商品列表

2026-02-18 16:32:40VUE

实现商品列表的基本结构

在Vue中实现商品列表,需要创建一个组件来展示商品数据。商品数据通常以数组形式存储,每个商品对象包含名称、价格、图片等属性。

<template>
  <div class="product-list">
    <div v-for="product in products" :key="product.id" class="product-item">
      <img :src="product.image" :alt="product.name">
      <h3>{{ product.name }}</h3>
      <p>价格: {{ product.price }}</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      products: [
        { id: 1, name: '商品1', price: 100, image: 'image1.jpg' },
        { id: 2, name: '商品2', price: 200, image: 'image2.jpg' },
        { id: 3, name: '商品3', price: 300, image: 'image3.jpg' }
      ]
    }
  }
}
</script>

<style>
.product-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
}
.product-item {
  border: 1px solid #ddd;
  padding: 15px;
  border-radius: 5px;
}
</style>

从API获取商品数据

实际项目中,商品数据通常从后端API获取。可以使用axios或fetch来请求数据。

vue 实现商品列表

<script>
import axios from 'axios';

export default {
  data() {
    return {
      products: []
    }
  },
  async created() {
    try {
      const response = await axios.get('https://api.example.com/products');
      this.products = response.data;
    } catch (error) {
      console.error('获取商品数据失败:', error);
    }
  }
}
</script>

添加商品筛选功能

为商品列表添加筛选功能,如按价格范围或类别筛选。

<template>
  <div>
    <div class="filters">
      <input v-model="minPrice" type="number" placeholder="最低价格">
      <input v-model="maxPrice" type="number" placeholder="最高价格">
    </div>
    <div class="product-list">
      <div v-for="product in filteredProducts" :key="product.id" class="product-item">
        <!-- 商品内容 -->
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      products: [],
      minPrice: null,
      maxPrice: null
    }
  },
  computed: {
    filteredProducts() {
      return this.products.filter(product => {
        const meetsMin = this.minPrice ? product.price >= this.minPrice : true;
        const meetsMax = this.maxPrice ? product.price <= this.maxPrice : true;
        return meetsMin && meetsMax;
      });
    }
  }
}
</script>

实现分页功能

对于大量商品数据,添加分页功能提升用户体验。

vue 实现商品列表

<template>
  <div>
    <div class="product-list">
      <!-- 商品列表 -->
    </div>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      products: [],
      currentPage: 1,
      itemsPerPage: 10
    }
  },
  computed: {
    paginatedProducts() {
      const start = (this.currentPage - 1) * this.itemsPerPage;
      const end = start + this.itemsPerPage;
      return this.products.slice(start, end);
    },
    totalPages() {
      return Math.ceil(this.products.length / this.itemsPerPage);
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++;
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
      }
    }
  }
}
</script>

添加商品排序功能

允许用户按价格、名称等属性对商品排序。

<template>
  <div>
    <select v-model="sortBy">
      <option value="price">按价格排序</option>
      <option value="name">按名称排序</option>
    </select>
    <div class="product-list">
      <div v-for="product in sortedProducts" :key="product.id">
        <!-- 商品内容 -->
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      products: [],
      sortBy: 'price'
    }
  },
  computed: {
    sortedProducts() {
      return [...this.products].sort((a, b) => {
        if (this.sortBy === 'price') {
          return a.price - b.price;
        } else {
          return a.name.localeCompare(b.name);
        }
      });
    }
  }
}
</script>

添加商品到购物车功能

实现将商品添加到购物车的功能,需要创建购物车状态管理。

<template>
  <div class="product-item">
    <!-- 商品内容 -->
    <button @click="addToCart(product)">加入购物车</button>
  </div>
</template>

<script>
export default {
  methods: {
    addToCart(product) {
      this.$store.commit('addToCart', product);
      // 或使用事件总线 this.$emit('add-to-cart', product);
    }
  }
}
</script>

对于更复杂的应用,建议使用Vuex管理购物车状态。

标签: 商品列表
分享给朋友:

相关文章

vue实现商品切换

vue实现商品切换

实现商品切换功能 在Vue中实现商品切换功能通常涉及数据绑定、事件处理和动态渲染。以下是几种常见方法: 数据驱动切换 创建商品数据数组和当前索引,通过修改索引实现切换: data() { re…

vue实现列表页面

vue实现列表页面

Vue 列表页面实现方案 基础列表渲染 使用 v-for 指令渲染数组数据,配合 :key 提升性能: <template> <ul> <li v-for=…

vue实现商品卡片

vue实现商品卡片

Vue 实现商品卡片 在 Vue 中实现商品卡片可以通过组件化的方式完成,以下是一个完整的实现方案: 商品卡片组件 <template> <div class="product…

vue 实现列表全选

vue 实现列表全选

实现列表全选功能 在Vue中实现列表全选功能,可以通过绑定数据和事件处理来实现。以下是一个完整的示例代码: <template> <div> <input…

vue实现商品累加

vue实现商品累加

Vue实现商品累加功能 商品累加功能通常用于购物车或商品列表,允许用户增减商品数量。以下是实现方法: 数据绑定与基础累加 在Vue组件中定义商品数据,使用v-model绑定数量输入框: data(…

vue实现商品橱窗

vue实现商品橱窗

实现商品橱窗的基本结构 使用Vue.js创建商品橱窗需要先搭建基本结构。商品橱窗通常包含商品列表、商品卡片、筛选和排序功能。在Vue项目中,可以通过组件化方式实现这些功能。 创建商品列表组件,用于展…