当前位置:首页 > VUE

vue实现商品的渲染

2026-01-21 22:25:24VUE

实现商品列表渲染

使用Vue.js渲染商品列表需要结合数据绑定和循环指令。以下是一个基本实现方式:

<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>

<script>
export default {
  data() {
    return {
      products: [
        { id: 1, name: '商品1', price: 100, image: 'product1.jpg' },
        { id: 2, name: '商品2', price: 200, image: 'product2.jpg' }
      ]
    }
  },
  methods: {
    addToCart(product) {
      // 添加到购物车逻辑
    }
  }
}
</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;
  text-align: center;
}
</style>

从API获取商品数据

实际项目中通常从后端API获取商品数据:

vue实现商品的渲染

<script>
export default {
  data() {
    return {
      products: [],
      isLoading: false
    }
  },
  async created() {
    this.isLoading = true
    try {
      const response = await fetch('https://api.example.com/products')
      this.products = await response.json()
    } catch (error) {
      console.error('获取商品失败:', error)
    } finally {
      this.isLoading = false
    }
  }
}
</script>

商品筛选功能

添加商品筛选功能提升用户体验:

vue实现商品的渲染

<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>

    <div v-if="filteredProducts.length === 0">没有找到匹配的商品</div>
    <ProductList :products="filteredProducts" />
  </div>
</template>

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

商品分页展示

处理大量商品时实现分页功能:

<template>
  <div>
    <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 {
      currentPage: 1,
      itemsPerPage: 10,
      allProducts: [] // 从API获取
    }
  },
  computed: {
    paginatedProducts() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      return this.allProducts.slice(start, start + this.itemsPerPage)
    },
    totalPages() {
      return Math.ceil(this.allProducts.length / this.itemsPerPage)
    }
  }
}
</script>

商品详情页实现

点击商品跳转到详情页的实现:

// 路由配置
const routes = [
  { path: '/products', component: ProductList },
  { path: '/products/:id', component: ProductDetail }
]

// ProductList.vue
<template>
  <div v-for="product in products" :key="product.id">
    <router-link :to="`/products/${product.id}`">
      {{ product.name }}
    </router-link>
  </div>
</template>

// ProductDetail.vue
<script>
export default {
  data() {
    return {
      product: null
    }
  },
  async created() {
    const productId = this.$route.params.id
    const response = await fetch(`https://api.example.com/products/${productId}`)
    this.product = await response.json()
  }
}
</script>

这些实现方式涵盖了商品展示的主要场景,可根据实际项目需求进行调整和扩展。

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

相关文章

vue中如何实现循环

vue中如何实现循环

在 Vue 中实现循环 Vue 提供了 v-for 指令用于实现循环渲染列表数据。v-for 可以遍历数组、对象或数字范围,生成动态的 DOM 元素。 遍历数组 使用 v-for 遍历数组时,语法为…

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template> &…

vue 实现树状

vue 实现树状

Vue 实现树状结构的方法 递归组件实现树状结构 递归组件是 Vue 中实现树状结构的常见方法。通过组件自身调用自身,可以轻松构建多层级树状结构。 <template> <ul…

vue实现Siri

vue实现Siri

Vue 实现 Siri 风格语音交互 实现类似 Siri 的语音交互功能需要结合语音识别、语音合成和前端交互逻辑。Vue 作为前端框架,可以很好地管理这些功能的交互状态。 语音识别集成 使用 Web…

vue 实现拖拉

vue 实现拖拉

Vue 实现拖拽功能的方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue实现换页

vue实现换页

Vue实现页面切换的方法 在Vue中实现页面切换通常可以通过Vue Router来完成。Vue Router是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。以下是几种常见的实现方式:…