鱼少学习多摸

day90 - GitHub搜索案例--大更新

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"/>&nbsp;
       <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组件

 
posted @   北海之上  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
/* 粒子吸附*/
点击右上角即可分享
微信分享提示