本代码用于构建一个交互式股票信息展示页面,允许用户浏览不同主题和趋势股票的信息。该页面可以集成到金融类应用程序或网站中,为用户提供便捷的股票信息查询和发现服务。
本代码主要实现了以下功能:
首先,需要准备股票信息数据。本代码使用了一个模拟的数据源,但实际应用中可以从 API 或数据库中获取真实数据。
const data = ref({
themes: [
{
title: "Top 20 Stocks",
description: "A curated list of the top 20 stocks by market capitalization.",
image: "https://source.unsplash.com/random/200x200",
},
// ... other themes
],
trending: [
{
title: "Meta",
description: "A social media company that owns Facebook, Instagram, and WhatsApp.",
image: "https://source.unsplash.com/random/200x200",
change: "+2.11%",
},
// ... other trending stocks
],
topGainers: [
{
title: "Apple",
description: "A technology company that makes iPhones, iPads, and Macs.",
image: "https://source.unsplash.com/random/200x200",
change: "+2.11%",
},
// ... other top gainers
],
});
使用 Vue.js 构建页面 UI,使用 VanUI 组件库实现卡片设计。
<template>
<div class="bg-gray-100">
<!-- Header -->
<div class="flex items-center justify-between px-4 py-3 border-b border-gray-200">
<h1 class="text-lg font-semibold text-gray-900">Discover</h1>
<div class="flex items-center">
<van-icon name="search" class="text-gray-500" />
<input type="text" placeholder="Search stocks..." class="ml-2 border-none outline-none text-gray-700" />
</div>
</div>
<!-- Themes -->
<div class="px-4 py-5">
<h2 class="text-base font-semibold text-gray-900">Themes</h2>
<div class="grid grid-cols-2 gap-4 mt-4">
<!-- Theme card -->
<van-card
class="flex items-center justify-between p-4 border border-gray-200 rounded-lg"
:thumb="data.themes[0].image"
>
<div>
<h3 class="text-base font-semibold text-gray-900">{{ data.themes[0].title }}</h3>
<p class="text-sm text-gray-500">{{ data.themes[0].description }}</p>
</div>
<van-icon name="arrow-right" class="text-gray-500" />
</van-card>
<!-- ... other theme cards -->
</div>
</div>
<!-- Trending -->
<div class="px-4 py-5">
<h2 class="text-base font-semibold text-gray-900">Trending</h2>
<div class="grid grid-cols-3 gap-4 mt-4">
<!-- Trending card -->
<van-card
class="flex items-center justify-between p-4 border border-gray-200 rounded-lg"
:thumb="data.trending[0].image"
>
<div>
<h3 class="text-base font-semibold text-gray-900">{{ data.trending[0].title }}</h3>
<p class="text-sm text-gray-500">{{ data.trending[0].description }}</p>
</div>
<div class="flex items-center">
<van-icon name="arrow-up" class="text-green-500" />
<span class="text-green-500 ml-1">{{ data.trending[0].change }}</span>
</div>
</van-card>
<!-- ... other trending cards -->
</div>
</div>
<!-- Top Gainers -->
<div class="px-4 py-5">
<h2 class="text-base font-semibold text-gray-900">Top gainers</h2>
<div class="grid grid-cols-4 gap-4 mt-4">
<!-- Top gainer card -->
<van-card
class="flex items-center justify-between p-4 border border-gray-200 rounded-lg"
:thumb="data.topGainers[0].image"
>
<div>
<h3 class="text-base font-semibold text-gray-900">{{ data.topGainers[0].title }}</h3>
<p class="text-sm text-gray-500">{{ data.topGainers[0].description }}</p>
</div>
<div class="flex items-center">
<van-icon name="arrow-up" class="text-green-500" />
<span class="text-green-500 ml-1">{{ data.topGainers[0].change }}</span>
</div>
</van-card>
<!-- ... other top gainer cards -->
</div>
</div>
</div>
</template>
使用 Vue.js 的 v-model
实现搜索功能,根据输入的关键字过滤股票信息。
<input type="text" placeholder="Search stocks..." v-model="searchQuery" class="ml-2 border-none outline-none text-gray-700" />
export default {
setup() {
const searchQuery = ref('');
const filteredThemes = computed(() => {
return data.value.themes.filter((theme) => {
return theme.title.toLowerCase().includes(searchQuery.value.toLowerCase());
});
});
const filteredTrending = computed(() => {
return data.value.trending.filter((stock) => {
return stock.title.toLowerCase().includes(searchQuery.value.toLowerCase());
});
});
const filteredTopGainers = computed(() => {
return data.value.topGainers.filter((stock) => {
return stock.title.toLowerCase().includes(searchQuery.value.toLowerCase());
});
});
return {
searchQuery,
filteredThemes,
filteredTrending,
filteredTopGainers,
};
},
};
本代码实现了一个功能丰富的股票信息展示页面,为用户提供了便捷的股票信息查询和发现服务。该代码具有良好的可扩展性,可以根据实际需求添加更多功能,例如:
未来,该代码还可以与其他金融服务集成,例如股票交易或投资建议,为用户提供更加全面的股票投资体验。