卡片列表是一种常见且通用的用户界面元素,用于展示一系列信息。它们可以用于各种应用程序,例如任务管理、笔记、联系人列表等。
此代码段是一个 Vue.js 组件,它渲染一个卡片列表。每个卡片由一个标题和一个描述组成。组件使用 v-for
指令动态地从 listData
数组中渲染卡片。
import { ref } from 'vue'
我们导入 Vue 的 ref
函数,用于创建可变的响应式状态。
const listData = ref([
{
title: 'List Action',
description: 'And here’s some amazing content. It’s very engaging. Right?',
},
{
title: 'List Action',
description: 'And here’s some amazing content. It’s very engaging. Right?',
},
{
title: 'List Action',
description: 'And here’s some amazing content. It’s very engaging. Right?',
},
{
title: 'List Action',
description: 'And here’s some amazing content. It’s very engaging. Right?',
},
])
我们使用 ref
创建一个名为 listData
的响应式数组,其中包含要渲染的卡片数据。
<template>
<div class="bg-gray-100">
<div class="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8">
<div class="px-4 py-6 sm:px-0" v-for="item in listData" :key="item.title">
<div class="border-4 border-dashed border-gray-200 rounded-lg h-96"></div>
</div>
</div>
</div>
</template>
我们使用 v-for
指令遍历 listData
数组并为每个项目渲染一个卡片。每个卡片是一个带边框的圆角矩形。
开发过程中的经验与收获
ref
和 v-for
来动态渲染数据。未来该卡片功能的拓展与优化