卡片组件广泛应用于各种 Web 和移动应用程序中,用于展示结构化信息,如产品、新闻、活动等。
该卡片组件提供以下基本功能:
npm install vant
在 Vue 文件中,导入 Vant 的卡片组件:
import { Card } from 'vant'
在模板中,使用 van-card
组件:
<van-card shadow="always" :style="{ height: '100px' }">
<template #header>
<div class="flex items-center justify-between">
<div class="text-lg font-semibold">Scrambled Eggs Toast</div>
<van-icon name="heart" color="#ff5722" />
</div>
</template>
<template #content>
<div class="flex items-center justify-between">
<div class="w-1/2">
<img
src="https://source.unsplash.com/random/200x200"
alt=""
class="rounded-full"
/>
</div>
<div class="w-1/2">
<p class="text-sm text-gray-500">
Scrambled eggs are a quick and easy breakfast option that can be
made with just a few ingredients. They are perfect for busy
mornings when you don't have a lot of time to cook.
</p>
</div>
</div>
</template>
</van-card>
可以使用 #header
和 #content
槽来自定义卡片的内容:
#header
槽用于放置标题和图标#content
槽用于放置卡片主体内容组件采用响应式设计,可以自动适应不同屏幕尺寸。
开发这段代码的过程让我深入了解了 Vue 3 和 Vant 组件的使用。我掌握了如何创建和自定义卡片组件,这将极大地提高我开发 Web 和移动应用程序的效率。
未来,该卡片组件可以进一步扩展和优化: