day90 - GitHub搜索案例--大更新
使用github的一个用户数据接口,输入名字进行搜索,返回其中30位用户
点击其头像或者名字可以跳转到指定的github界面
分为两个组件:搜索组件和列表组件,二者进行通信
list组件
利用全局事件总线传递数据,显示列表成员
<template> <div class="row"> <!-- 展示用户列表--> <div v-show="info.users.length" class="card" v-for="user in info.users" :key="user.login"> <a :href="user.html_url" target="_blank"> <img :src="user.avatar_url" style='width: 100px'/> </a> <p class="card-text">{{user.login}}</p> </div> <!-- 展示欢迎词--> <h1 v-show="info.isFirst">Welcome!</h1> <!-- 展示加载中--> <h1 v-show="info.isLoading">Loading...</h1> <!-- 展示错误信息--> <transition name="animate__animated animate__bounce" appear enter-active-class="animate__rubberBand"> <h1 v-show="info.errMsg">{{ info.errMsg }}</h1> </transition> </div> </template> <script> import 'animate.css' export default { name: "ListCom", data(){ return{ info:{ isFirst:true, isLoading:false, errMsg:'', users:[] } } }, mounted() { this.$bus.$on('updateList',(dataObj)=>{ // console.log(dataObj) // this.isFirst = isFirst // this.isLoading = isLoading // this.errmsg = errmsg // this.users = users this.info = {...this.info,...dataObj} console.log(this) }) } } </script> <style scoped> .album { min-height: 50rem; /* Can be removed; just added for demo purposes */ padding-top: 3rem; padding-bottom: 3rem; background-color: #f7f7f7; } .card { float: left; width: 33.333%; padding: .75rem; margin-bottom: 2rem; border: 1px solid #efefef; text-align: center; } .card > img { margin-bottom: .75rem; border-radius: 100px; } .card-text { font-size: 85%; } </style>
search组件
利用axios插件对github的用户接口进行数据访问
在得到的结果中利用全局事件总线向list组件传递数据,对列表进行更新
<template> <section class="jumbotron"> <h3 class="jumbotron-heading">Search Github Users</h3> <div> <input type="text" placeholder="enter the name you search" v-model="keyWord"/> <button @click="searchUsers">Search</button> </div> </section> </template> <script> import axios from "axios"; export default { name: "SearchCom", data(){ return{ keyWord:'' } }, methods:{ searchUsers(){ //请求前更新list数据 this.$bus.$emit('updateList', {isFirst:false, isLoading:true, errMsg:'', users:[]}) axios.get(`https://api.github.com/search/users?q=${this.keyWord}`).then( response =>{ console.log('okk') // this.$bus.$emit('updateList',response.data.items) // this.$bus.$emit('updateList',false,false,'',response.data.items) //请求成功后更新list数据 this.$bus.$emit('updateList', {isLoading:false, errMsg:'', users:response.data.items}) }, error =>{ console.log('error',error.message) //请求失败后更新list数据 this.$bus.$emit('updateList', {isLoading:false, errMsg:error.message, users:[]}) }, ) } } } </script> <style scoped> </style>
app组件
简单的展示list和search组件
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗