当前位置:首页 > VUE

vue 实现商品列表

2026-01-18 00:02:55VUE

实现商品列表的基本结构

在Vue中实现商品列表,通常需要创建一个组件来展示商品数据。可以使用v-for指令循环渲染商品列表,并通过数据绑定动态显示商品信息。

<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>
      <button @click="addToCart(product)">加入购物车</button>
    </div>
  </div>
</template>

定义商品数据

商品数据可以存储在组件的data属性中,或者通过API从后端获取。以下是静态数据的示例:

<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' }
      ]
    }
  },
  methods: {
    addToCart(product) {
      console.log('添加商品:', product.name)
    }
  }
}
</script>

动态加载商品数据

如果需要从后端API加载商品数据,可以使用axios或其他HTTP库。在createdmounted钩子中发起请求:

<script>
import axios from 'axios'

export default {
  data() {
    return {
      products: []
    }
  },
  mounted() {
    axios.get('/api/products')
      .then(response => {
        this.products = response.data
      })
      .catch(error => {
        console.error('加载商品数据失败:', error)
      })
  }
}
</script>

样式设计

使用CSS为商品列表添加样式,确保布局美观且响应式:

<style>
.product-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
  padding: 20px;
}

.product-item {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 15px;
  text-align: center;
}

.product-item img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
}

.product-item h3 {
  margin: 10px 0;
  font-size: 1.2em;
}

.product-item p {
  color: #e63946;
  font-weight: bold;
}

.product-item button {
  background-color: #007bff;
  color: white;
  border: none;
  padding: 8px 15px;
  border-radius: 4px;
  cursor: pointer;
}

.product-item button:hover {
  background-color: #0056b3;
}
</style>

分页功能

如果商品数量较多,可以添加分页功能。使用v-pagination组件或自定义分页逻辑:

<template>
  <div>
    <product-list :products="paginatedProducts" />
    <div class="pagination">
      <button 
        v-for="page in totalPages" 
        :key="page" 
        @click="currentPage = page"
        :class="{ active: currentPage === page }"
      >
        {{ page }}
      </button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      products: [], // 从API加载的数据
      currentPage: 1,
      itemsPerPage: 10
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.products.length / this.itemsPerPage)
    },
    paginatedProducts() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.products.slice(start, end)
    }
  }
}
</script>

商品搜索与筛选

添加搜索框和筛选功能,提升用户体验:

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索商品" />
    <select v-model="selectedCategory">
      <option value="">所有分类</option>
      <option v-for="category in categories" :value="category">
        {{ category }}
      </option>
    </select>
    <product-list :products="filteredProducts" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      selectedCategory: '',
      categories: ['电子产品', '服装', '家居']
    }
  },
  computed: {
    filteredProducts() {
      return this.products.filter(product => {
        const matchesSearch = product.name.toLowerCase().includes(
          this.searchQuery.toLowerCase()
        )
        const matchesCategory = !this.selectedCategory || 
          product.category === this.selectedCategory
        return matchesSearch && matchesCategory
      })
    }
  }
}
</script>

vue 实现商品列表

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

相关文章

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue列表查询实现

vue列表查询实现

实现 Vue 列表查询功能 基础列表渲染 在 Vue 中可以通过 v-for 指令实现列表渲染,结合计算属性动态过滤数据: <template> <div> &…

vue实现搜索列表

vue实现搜索列表

Vue 实现搜索列表 在 Vue 中实现搜索列表功能,通常需要结合数据绑定、计算属性和事件监听。以下是一个完整的实现方案: 数据准备与模板结构 <template> <div&…

vue实现商品列表

vue实现商品列表

Vue实现商品列表的方法 使用Vue实现商品列表需要结合数据绑定、组件化和状态管理。以下是实现商品列表的几种常见方法: 基础数据绑定实现 在Vue组件中定义商品数据数组,使用v-for指令循环渲染商…

vue实现列表水印

vue实现列表水印

实现列表水印的方法 在Vue中为列表添加水印可以通过多种方式实现,以下是几种常见的方法: 使用CSS背景图 通过CSS的background-image属性为列表元素添加水印背景。水印可以是文字或…

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 无限滚动列表通常用于展示大量数据,通过动态加载数据减少初始渲染压力。以下是基于Vue的实现方法: 使用Intersection Observer API监听滚动 Inters…