Vue 3 实现与使用超好看卡片组件,附完整代码示例!
以下是一个使用 Vue 3 编写的好看的卡片组件示例。这个卡片包含、描述和一个行动按钮,并且有一定的样式美化。 ```vue <template> <!-- 卡片容器 --> <div class="card"> <!-- 卡片 --> <h2 class="card-title">{{ title }}</h2> <!-- 卡片描述 --> <p class="card-description">{{ description }}</p> <!-- 行动按钮 --> <button class="card-button" @click="onButtonClick">{{ buttonText }}</button> </div> </template> <script setup> import { ref } from 'vue'; // 定义组件的 props const props = defineProps({ // 卡片 title: { type: String, default: 'Default Title' }, // 卡片描述 description: { type: String, default: 'Default description text.' }, // 按钮文本 buttonText: { type: String, default: 'Click Me' } }); // 定义按钮点击事件处理函数 const onButtonClick = () => { console.log('Button clicked!'); }; </script> <style scoped> .card { /* 卡片的宽度 */ width: 300px; /* 卡片的内边距 */ padding: 20px; /* 卡片的外边距 */ margin: 20px; /* 卡片的颜色 */ background-color: #fff; /* 卡片的边框 */ border: 1px solid #e0e0e0; /* 卡片的圆角 */ border-radius: 8px; /* 卡片的阴影 */ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 文本居中 */ text-align: center; } .card-title { /* 的字体大小 */ font-size: 24px; /* 的字体粗细 */ font-weight: bold; /* 的下边距 */ margin-bottom: 10px; } .card-description { /* 描述的字体大小 */ font-size: 16px; /* 描述的颜色 */ color: #666; /* 描述的下边距 */ margin-bottom: 20px; } .card-button { /* 按钮的颜色 */ background-color: #007bff; /* 按钮的文字颜色 */ color: #fff; /* 按钮的内边距 */ padding: 10px 20px; /* 按钮的边框 */ border: none; /* 按钮的圆角 */ border-radius: 4px; /* 鼠标悬停时的光标样式 */ cursor: pointer; /* 鼠标悬停时的颜色 */ transition: background-color 0.3s ease; } .card-button:hover { /* 鼠标悬停时的颜色 */ background-color: #0056b3; } </style> ``` 你可以将上述代码保存为一个 `.vue` 文件,然后在你的 Vue 3 项目中使用这个组件。例如: ```vue <template> <div> <!-- 使用自定义的卡片组件 --> <Card title="My Card" description="This is a description of the card." buttonText="Learn More" /> </div> </template> <script setup> import Card from './Card.vue'; </script> ``` [2026-02-12 10:47:56 | AI写代码神器 | 884点数解答]
- [讯飞星火] "Debugging 'Cannot Read Properties of Null (reading \'userid\')' Error in Vue Components: Steps and Example"(614点数解答 | 2024-11-28 14:31:57)253
- [阿里通义] Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(261点数解答 | 2024-11-28 21:19:39)316
- [字节豆包] JSP 实现商品展示模块:分页、搜索、交互与美观界面全搞定!(19点数解答 | 2024-12-13 15:00:43)316
- [讯飞星火] "Debugging 'Cannot Read Properties of Null (Reading 'userid')' Error: A Step-by-Step Guide"(523点数解答 | 2024-11-28 14:32:05)239
- [讯飞星火] 解决 “Cannot read properties of null (reading 'userid')” 错误的代码修改方案(516点数解答 | 2024-11-28 14:32:43)266
- [讯飞星火] Vue.js 与 Matter.js 碰撞:轻松实现物理模拟全攻略 (522点数解答 | 2024-05-29 22:26:06)401
- [阿里通义] 速览!Vue 父组件与子组件生命周期钩子执行顺序全解析(226点数解答 | 2023-11-09 01:49:24)259
- [字节豆包] 全方位攻略:打造 Element UI 中完美方形 el - card 设计 (296点数解答 | 2024-10-10 14:13:41)368
- 用 JS 中 for 循环实现 1 到 100 相加并输出结果到页面的完整代码 (240点数解答 | 2024-05-20 22:11:29)423
- 用 JS 的 while 循环实现 1 到 100 相加并输出到页面的代码揭秘(47点数解答 | 2024-05-20 22:16:48)335
- [GPT] 探寻数组中最长摆动子序列长度:思路剖析与代码优化(758点数解答 | 2024-12-23 23:18:29)247
- [GPT] 独家剖析:求解数组最长摆动子序列长度的代码实现与改进建议(350点数解答 | 2024-12-23 23:20:54)244