当前位置:首页 > VUE

vue实现双列商品卡片

2026-01-21 18:39:46VUE

Vue 实现双列商品卡片

使用 Vue 实现双列商品卡片可以通过 flex 布局或 grid 布局实现,以下是两种常见方法:

方法一:使用 Flex 布局

<template>
  <div class="product-list">
    <div v-for="product in products" :key="product.id" class="product-card">
      <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' },
        // 更多商品数据...
      ]
    }
  }
}
</script>

<style>
.product-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 10px; /* 可选,卡片间距 */
}

.product-card {
  width: calc(50% - 5px); /* 减去间距的一半 */
  border: 1px solid #eee;
  padding: 10px;
  box-sizing: border-box;
}
</style>

方法二:使用 Grid 布局

<template>
  <div class="product-grid">
    <div v-for="product in products" :key="product.id" class="product-card">
      <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' },
        // 更多商品数据...
      ]
    }
  }
}
</script>

<style>
.product-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px; /* 卡片间距 */
}

.product-card {
  border: 1px solid #eee;
  padding: 10px;
}
</style>

响应式调整

如果需要在小屏幕设备上显示单列,可以添加媒体查询:

@media (max-width: 600px) {
  .product-list {
    flex-direction: column;
  }
  .product-card {
    width: 100%;
  }

  /* 或针对 Grid 布局 */
  .product-grid {
    grid-template-columns: 1fr;
  }
}

动态加载数据

如果商品数据是通过 API 获取的,可以使用 axiosfetch

vue实现双列商品卡片

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

以上方法均可实现双列商品卡片布局,根据项目需求选择合适的方式。

标签: 卡片商品
分享给朋友:

相关文章

vue实现商品全选

vue实现商品全选

实现全选功能的基本思路 在Vue中实现商品全选功能,通常需要维护一个商品列表数据和一个选中状态数组。通过计算属性判断是否全选,通过方法控制全选或取消全选。 定义数据模型 data() {…

vue实现商品卡片

vue实现商品卡片

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

vue实现删除卡片

vue实现删除卡片

实现删除卡片功能 在Vue中实现删除卡片功能通常涉及以下几个关键步骤: 数据绑定与列表渲染 使用v-for指令渲染卡片列表,确保每个卡片有唯一标识符(通常为id): <div v-for="…

vue实现商品累加

vue实现商品累加

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

vue实现商品筛选

vue实现商品筛选

实现商品筛选功能 在Vue中实现商品筛选功能通常涉及以下几个关键步骤: 数据准备 定义商品数据数组和筛选条件。商品数据应包含各种属性如价格、类别、品牌等。 data() { return {…

vue element 实现商品sku

vue element 实现商品sku

商品SKU实现方法 在Vue和Element UI中实现商品SKU功能,可以通过以下步骤完成: 数据结构设计 商品SKU通常需要处理多个属性组合,每个属性有不同的选项。数据结构可以设计为: dat…