当前位置:首页 > VUE

vue实现购物节

2026-01-15 02:54:57VUE

Vue 实现购物节功能

商品展示组件

创建商品卡片组件展示促销商品信息,包含图片、名称、原价、折扣价和倒计时。

<template>
  <div class="product-card">
    <img :src="product.image" :alt="product.name">
    <h3>{{ product.name }}</h3>
    <p class="original-price">¥{{ product.price }}</p>
    <p class="discount-price">¥{{ product.discountPrice }}</p>
    <div class="countdown">
      剩余时间: {{ countdown.days }}天 {{ countdown.hours }}:{{ countdown.minutes }}:{{ countdown.seconds }}
    </div>
    <button @click="addToCart">加入购物车</button>
  </div>
</template>

倒计时功能

使用计算属性和定时器实现活动倒计时功能。

data() {
  return {
    endTime: new Date('2023-11-11 23:59:59').getTime(),
    now: Date.now(),
    timer: null
  }
},
computed: {
  countdown() {
    const diff = this.endTime - this.now
    const days = Math.floor(diff / (1000 * 60 * 60 * 24))
    const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
    const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
    const seconds = Math.floor((diff % (1000 * 60)) / 1000)
    return { days, hours, minutes, seconds }
  }
},
mounted() {
  this.timer = setInterval(() => {
    this.now = Date.now()
  }, 1000)
},
beforeDestroy() {
  clearInterval(this.timer)
}

购物车功能

使用Vuex管理购物车状态,实现添加商品和计算总价功能。

// store.js
const store = new Vuex.Store({
  state: {
    cart: []
  },
  mutations: {
    ADD_TO_CART(state, product) {
      const existingItem = state.cart.find(item => item.id === product.id)
      if (existingItem) {
        existingItem.quantity++
      } else {
        state.cart.push({ ...product, quantity: 1 })
      }
    }
  },
  getters: {
    cartTotal(state) {
      return state.cart.reduce((total, item) => {
        return total + (item.discountPrice * item.quantity)
      }, 0)
    }
  }
})

优惠券功能

实现优惠券选择和使用逻辑。

<template>
  <div class="coupon-selector">
    <select v-model="selectedCoupon">
      <option v-for="coupon in coupons" :value="coupon" :key="coupon.id">
        {{ coupon.name }} (减¥{{ coupon.value }})
      </option>
    </select>
    <p>优惠后总价: ¥{{ discountedTotal }}</p>
  </div>
</template>

<script>
export default {
  computed: {
    discountedTotal() {
      return this.$store.getters.cartTotal - (this.selectedCoupon ? this.selectedCoupon.value : 0)
    }
  }
}
</script>

页面布局

使用Vue Router创建活动页面和商品详情页。

const routes = [
  {
    path: '/promotion',
    component: PromotionPage,
    children: [
      {
        path: 'product/:id',
        component: ProductDetail
      }
    ]
  }
]

响应式设计

使用CSS媒体查询确保在不同设备上良好显示。

@media (max-width: 768px) {
  .product-card {
    width: 100%;
    margin-bottom: 20px;
  }
}

性能优化

使用Vue的异步组件和懒加载提升页面加载速度。

vue实现购物节

const ProductDetail = () => import('./components/ProductDetail.vue')

标签: vue
分享给朋友:

相关文章

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独立…

vue实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-m…

vue 实现排序

vue 实现排序

Vue 实现排序的方法 使用计算属性实现排序 通过计算属性对数组进行排序,可以保持原始数据不变。示例代码展示了如何对列表按名称升序排序: <template> <div>…

vue mvvm 实现

vue mvvm 实现

Vue MVVM 实现原理 Vue.js 的 MVVM(Model-View-ViewModel)架构通过数据绑定和响应式系统实现视图与数据的自动同步。以下是其核心实现机制: 数据劫持与响应式系统…

vue实现梯形

vue实现梯形

在Vue中实现梯形效果,可以通过CSS的transform属性或clip-path属性来实现。以下是几种常见的实现方法: 使用CSS transform实现梯形 通过CSS的transform: s…