当前位置: 首页 > news >正文

50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | GithubProfies(GitHub 个人资料)

📅 我们继续 50 个小项目挑战!—— GithubProfies组件

仓库地址:https://github.com/SunACong/50-vue-projects

项目预览地址:https://50-vue-projects.vercel.app/

在这里插入图片描述


使用 Vue 3 的 Composition API(<script setup>)结合 TailwindCSS 创建一个Github 用户信息展示组件。用户可以在输入框中搜索用户名,组件将通过 Github Public API 获取用户信息和其仓库数据并展示。


🎯 组件目标

  • 用户输入 Github 用户名并提交
  • 通过 Github API 获取用户信息
  • 获取用户的公开仓库并展示
  • 支持错误提示(如用户不存在)
  • 使用 Vue 3 Composition API 管理状态
  • 使用 TailwindCSS 构建响应式 UI
  • 展示清晰的用户信息与仓库列表

⚙️ 技术实现点

技术点描述
Vue 3 <script setup>使用响应式变量管理用户信息、仓库、错误状态
ref 响应式变量控制 usernameuserreposerror
v-model绑定输入框内容
@submit.prevent阻止默认表单提交行为并触发请求
fetch()请求 Github 用户信息与仓库数据
错误处理使用 try/catch 捕获请求错误
TailwindCSS 布局类构建全屏布局、卡片样式、响应式设计
动态条件渲染 v-if, v-else-if根据状态显示错误、用户信息或空状态

🧱 组件实现

模板结构 <template>

<template><div class="flex min-h-screen flex-col items-center justify-center bg-[#2a2a72] p-4 font-sans text-white"><form @submit.prevent="handleSubmit" class="mb-8 w-full max-w-xl"><inputv-model="username"type="text"placeholder="Search a Github User"class="w-full rounded-lg bg-[#4c2885] p-4 text-white shadow-lg placeholder:text-[#bbb] focus:outline-none" /></form><!-- 错误卡片 --><div v-if="error" class="max-w-3xl rounded-2xl bg-[#4c2885] p-6 text-center shadow-lg"><h1 class="text-xl font-bold">{{ error }}</h1></div><!-- 用户信息卡片 --><div v-else-if="user" class="flex w-full max-w-3xl flex-col rounded-2xl bg-[#4c2885] p-6 shadow-lg md:flex-row"><div class="flex flex-shrink-0 justify-center"><img :src="user.avatar_url" :alt="user.name" class="h-36 w-36 rounded-full border-[10px] border-[#2a2a72]" /></div><div class="mt-4 ml-0 flex-grow md:mt-0 md:ml-8"><h2 class="mb-2 text-2xl font-bold">{{ user.name || user.login }}</h2><p v-if="user.bio" class="mb-4">{{ user.bio }}</p><ul class="mb-4 flex max-w-md justify-between text-sm"><li>{{ user.followers }}<strong class="ml-1">Followers</strong></li><li>{{ user.following }}<strong class="ml-1">Following</strong></li><li>{{ user.public_repos }}<strong class="ml-1">Repos</strong></li></ul><div class="flex flex-wrap gap-2"><a v-for="repo in repos.slice(0, 5)" :key="repo.id" :href="repo.html_url" target="_blank"class="inline-block rounded bg-[#212a72] px-2 py-1 text-xs text-white">{{ repo.name }}</a></div></div></div></div>
</template>

脚本逻辑 <script setup>

<script setup>
import { ref } from 'vue'const API_URL = 'https://api.github.com/users/'const username = ref('')
const user = ref(null)
const repos = ref([])
const error = ref('')const getUser = async (name) => {try {error.value = ''const res = await fetch(API_URL + name)if (!res.ok) {throw new Error('Not Found')}user.value = await res.json()console.log(user.value)await getRepos(name)} catch (err) {error.value = 'No profile with this username'user.value = nullrepos.value = []}
}const getRepos = async (name) => {try {const res = await fetch(`${API_URL}${name}/repos?sort=created`)if (!res.ok) {throw new Error('Repo Fetch Failed')}repos.value = await res.json()} catch (err) {error.value = 'Problem fetching repos'}
}const handleSubmit = () => {const trimmed = username.value.trim()if (trimmed) {getUser(trimmed)username.value = ''}
}
</script>

样式部分 <style scoped>

<style scoped>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;400&display=swap');
:root {font-family: 'Poppins', sans-serif;
}
</style>

🔍 重点效果实现

✅ 用户信息获取

通过 fetch() 请求 Github API 获取用户信息:

const res = await fetch(API_URL + name)

如果用户不存在,抛出错误并设置提示信息。

💡 获取用户仓库

使用 repos 接口获取用户公开仓库:

const res = await fetch(`${API_URL}${name}/repos?sort=created`)

并在页面上展示前 5 个仓库。

🎮 表单提交处理

使用 @submit.prevent 阻止默认提交行为并触发请求:

<form @submit.prevent="handleSubmit">

输入框使用 v-model 双向绑定用户名:

<input v-model="username" type="text" />

🎨 动态条件渲染

根据状态显示不同的内容:

<div v-if="error">...</div>
<div v-else-if="user">...</div>

🎨 TailwindCSS 样式重点讲解

类名作用
flex min-h-screen flex-col items-center justify-center全屏居中布局
bg-[#2a2a72], text-white设置主题颜色
rounded-lg, shadow-lg输入框与卡片阴影样式
border-[10px] border-[#2a2a72]头像边框样式
flex-wrap gap-2仓库标签自动换行与间距
text-xs, rounded, bg-[#212a72]仓库标签样式
max-w-3xl, md:flex-row响应式布局控制

这些 TailwindCSS 类帮助我们快速构建了一个美观、响应式的用户信息展示组件。


📁 常量定义 + 组件路由

constants/index.js 添加组件预览常量:

{id: 28,title: 'Github Profiles',image: 'https://50projects50days.com/img/projects-img/28-github-profiles.png',link: 'GithubProfiles',},

router/index.js 中添加路由选项:

{path: '/GithubProfiles',name: 'GithubProfiles',component: () => import('@/projects/GithubProfiles.vue'),},

🏁 总结

基于 Vue 3 和 TailwindCSS 的 Github 用户信息展示组件不仅实现了基本的用户搜索功能,还展示了如何使用 Vue 的响应式能力、异步请求和条件渲染构建一个完整的功能页面。

你可以进一步扩展此组件的功能包括:

  • ✅ 添加加载动画(如请求时显示 spinner)
  • ✅ 支持分页展示仓库
  • ✅ 添加本地缓存(如使用 localStorage)
  • ✅ 支持主题切换(暗色/亮色)
  • ✅ 将组件封装为 <GithubUserCard /> 可复用组件

👉 下一篇,我们将完成Double Click Heart组件,双击点赞的设计。🚀

感谢阅读,欢迎点赞、收藏和分享 😊

http://www.xdnf.cn/news/1119421.html

相关文章:

  • 编译器 VS 解释器
  • 电脑升级Experience
  • Linux操作系统之信号:信号的产生
  • 【C++进阶】---- 多态
  • 鹧鸪云:别墅光储项目方案设计的最终选择
  • 【Linux系统】进程切换 | 进程调度——O(1)调度队列
  • Linux:3_基础开发⼯具
  • 【Linux】基本指令详解(一) 树状文件结构、家目录、绝对/相对路径、linux文件类型
  • 使用systemctl命令控制软件的启动和关闭
  • 打破空间边界!Nas-Cab用模块化设计重构个人存储逻辑
  • 各种开发语言主要语法对比
  • Codeforces Round 1019 (Div. 2) A-D
  • GPU网络运维
  • UV vs Pip:Python 包管理的革命性进化
  • 【安卓笔记】进程和线程的基础知识
  • 实现高效、可靠的基于骨骼的人体姿态建模(第二章 基于三维人体姿态回归的语义图卷积网络)
  • 马蹄集 BD202401补给
  • Elasticsearch 9.x 升级变化
  • Swift 解 LeetCode 326:两种方法判断是否是 3 的幂,含循环与数学技巧
  • APK安装器(安卓端)一键解除VX限制!轻松安装各种手机应用
  • 一键获取android公钥/ios公钥工具
  • Java面试总结(经典题)(Java多线程)(一)
  • 基于Hadoop的竞赛网站日志数据分析与可视化(上)
  • 八、nginx搭建,实现vue跳转nginx跳转gateway
  • 基于hadoop的竞赛网站日志数据分析与可视化(下)
  • pycharm恢复出厂设置,可以解决大多数pycharm存在的问题
  • idea下无法打开sceneBulider解决方法
  • LINUX714 自动挂载/nfs;物理卷
  • Relocations in generic ELF (EM: 40)
  • OPENPPP2 VEthernet 网络协议堆栈(CTCP)VNetStack 深度技术解析