在电子商务网站中,产品卡片是展示产品信息和促成销售的关键元素。它通常包含产品名称、价格、描述、图片等信息,以及添加到购物车等操作按钮。
本代码展示了一个基于 Vue 的产品卡片组件,其基本功能包括:
在 <script>
标签中,使用 ref
钩子初始化产品数据和相关产品列表:
const data = ref({
product: {
name: 'Stockholm',
price: 'DKK 8.999',
description: 'Stockholm builds on the classic principles of Nordic design: High quality, timelessness,',
image: 'https://source.unsplash.com/random/600x800',
},
relatedProducts: [
{
name: 'Copenhagen',
price: 'DKK 3.999',
image: 'https://source.unsplash.com/random/300x400',
},
],
});
在 <template>
标签中,使用数据绑定语法渲染产品信息:
<div class="flex flex-col items-center justify-center">
<div class="text-5xl font-bold text-white">{{ data.product.name }}</div>
<div class="text-3xl font-bold text-white">{{ data.product.price }}</div>
<div class="text-2xl font-bold text-white">{{ data.product.description }}</div>
</div>
使用 van-icon
组件添加 "添加到购物车" 按钮:
<div class="flex items-center justify-between">
<div class="text-2xl font-bold text-black">STOCKHOLM</div>
<van-icon name="plus" class="text-black" />
</div>
在 <template>
标签中,使用 v-for
指令渲染相关产品列表:
<div class="flex items-center justify-between mt-10">
<div class="w-1/2">
<img
src="https://source.unsplash.com/random/600x800"
alt=""
class="rounded-lg shadow-lg"
/>
</div>
<div class="w-1/2 ml-10">
<div class="flex items-center justify-between">
<div class="text-2xl font-bold text-black">COPENHAGEN</div>
<van-icon name="plus" class="text-black" />
</div>
<div class="text-lg text-gray-600">
Stockholm builds on the classic principles of Nordic design: High
quality, timelessness,
</div>
<div class="flex items-center justify-between mt-10">
<div class="text-xl font-bold text-black">COPENHAGEN</div>
<div class="text-xl font-bold text-black">DKK 3.999</div>
</div>
</div>
</div>
开发过程中的经验与收获:
ref
钩子进行数据管理,简洁高效。v-for
指令实现列表渲染,代码简洁可维护。van-icon
),提升组件的视觉效果和用户体验。未来该卡片功能的拓展与优化: