在移动端应用中,卡片是一种常见且重要的交互元素,用于展示各种信息,例如待办事项、日程安排、地图定位等。本文将介绍如何使用 Vue.js 开发一个功能丰富的卡片组件,以满足移动端应用中各种场景的需求。
该卡片组件提供了以下基本功能:
1. 创建组件模板
<template>
<div class="bg-gray-100 h-screen">
<div class="bg-white shadow-md rounded px-4 pt-4 pb-5">
<!-- 卡片头部 -->
<div class="flex justify-between items-center">
<div class="text-xl font-semibold text-gray-800">findme</div>
<div class="flex items-center">
<van-icon name="close" class="text-gray-600 text-2xl" />
</div>
</div>
<!-- 搜索框 -->
<div class="mt-4">
<div class="flex items-center">
<van-search
v-model="search"
placeholder="Search"
class="w-full rounded-full px-4 py-2 border border-gray-300"
/>
<van-icon
name="close"
class="text-gray-600 text-lg ml-2"
@click="search = ''"
v-if="search"
/>
</div>
</div>
<!-- 分类标签页 -->
<div class="mt-4">
<van-tabs v-model="activeTab" class="text-gray-600">
<van-tab title="All Challenges" name="all" />
<van-tab title="Uncompleted" name="uncompleted" />
<van-tab title="Completed" name="completed" />
</van-tabs>
</div>
<!-- 搜索结果 -->
<div class="mt-4">
<div class="text-xl font-semibold text-gray-800">Search Result</div>
<div class="mt-4">
<!-- 卡片内容 -->
<div class="flex items-center">
<div
class="w-10 h-10 rounded-full bg-gray-300 flex items-center justify-center"
>
<img
:src="`https://source.unsplash.com/random/100x100`"
class="w-full h-full rounded-full"
/>
</div>
<div class="ml-4">
<div class="text-lg font-semibold text-gray-800">
CVS Pharmacy
</div>
<div class="text-sm text-gray-600">
Visit our store everyday for 10 days and get 2 stamps for each
visit! Collect them all and get a free 50% discount!
</div>
</div>
</div>
<!-- 进度条 -->
<div class="mt-4">
<van-progress
:percentage="50"
:show-text="false"
class="rounded-full h-2 bg-gray-200"
/>
</div>
<!-- 提示信息 -->
<div class="text-sm text-gray-600 mt-2">
Keep going!! You're halfway there.
</div>
</div>
</div>
</div>
<!-- 底部导航栏 -->
<van-nav-bar
title="findme"
left-text=""
left-arrow
fixed
:border="false"
class="mt-8"
>
<van-tabbar
v-model="activeTab"
class="van-tabbar-fixed"
:active-color="'#1989fa'"
:inactive-color="'#999999'"
>
<van-tabbar-item icon="home-o" to="/home"> Home </van-tabbar-item>
<van-tabbar-item icon="search" to="/search"> Search </van-tabbar-item>
<van-tabbar-item icon="card-o" to="/cards"> Cards </van-tabbar-item>
<van-tabbar-item icon="orders-o" to="/orders"> Orders </van-tabbar-item>
<van-tabbar-item icon="user-o" to="/profile"> Profile </van-tabbar-item>
</van-tabbar>
</van-nav-bar>
</div>
</template>
2. 定义组件脚本
<script lang="tsx" setup>
import { ref, onMounted } from "vue";
import * as echarts from "echarts";
import { h } from "vue";
import { createComponent } from "echarts-for-vue";
import { Calendar } from "v-calendar";
import { BMap } from "vue3-baidu-map-gl";
const ECharts = createComponent({ echarts, h });
const search = ref("");
const activeTab = ref("all");
onMounted(() => {
// 百度地图API
const bmap = new BMap({ ak: "cxtWZ9zGiLlLdQwBSuGGwpanwqfaLvEc" });
const point = new bmap.Point(116.404, 39.915);
const marker = new bmap.Marker(point); // 创建标注
bmap.addOverlay(marker); // 将标注添加到地图中
bmap.centerAndZoom(point, 12); // 初始化地图,设置中心点坐标和缩放级别
});
</script>
3. 实现搜索功能
const search = ref("");
4. 实现分类功能
const activeTab = ref("all");
5. 实现进度条功能
<van-progress
:percentage="50"
:show-text="false"
class="rounded-full h-2 bg-gray-200"
/>
6. 实现地图定位功能
<BMap :ak="ak" :center="center" :zoom="zoom" style="width: 100%; height: 200px;">
<b-marker :position="position" />
</BMap>
通过上述步骤,我们成功开发了一个功能丰富的卡片组件,满足了移动端应用中各种场景的需求。在开发过程中,我们学习了如何使用 Vue.js 构建组件、如何集成第三方库(如百度地图),以及如何处理用户交互。
未来,该卡片组件还可以进行以下拓展和优化: