github搜索案例(vue版本)之完善案例

代码变更涉及的文件有:

  1. App.vue,即App组件。
  2. components/Search.vue,即Search组件。
  3. components/List.vue,即List组件。

在这里插入图片描述

App.vue(App组件)

<template>
  <div id="app">
    <div class="container">
      <Search :updateInfo="updateInfo"/>
      <List :info="info"/>
    </div>
  </div>
</template>

<script>
import Search from "./components/Search.vue";
import List from './components/List.vue';

export default {
  name: 'App',
  components: {
    Search,
    List
  },
  data(){
    return {
      info:{
        users:[],
        isFirst:true,
        isLoading:false,
        errorMsg:""
      }
    }
  },
  methods:{
    updateInfo(dataObj){
      this.info = {...this.info,...dataObj}
    }
  }
}
</script>

Search.vue(Search组件)

<template>
    <section class="jumbotron">
      <h3 class="jumbotron-heading">搜索Github用户</h3>
      <div>
        <input type="text" placeholder="输入关键词" ref="keywordEle"/>&nbsp;
        <button @click="handleClick">搜索</button>
      </div>
    </section>  
</template>

<script>
import axios from "axios";

export default {
    name:"Search",
    props:["updateInfo"],
    methods:{
        handleClick(){
            const keyword = this.$refs.keywordEle.value;
            this.updateInfo({
                isFirst:false,
                isLoading:true,
                users:[],
                errorMsg:""
            })
            axios.get(`https://api.github.com/search/users?q=${keyword}`).then(
                response => {
                    this.updateInfo({
                        isLoading:false,
                        users:response.data.items,
                        errorMsg:""
                    })
                },
                error => {
                    this.updateInfo({
                        isLoading:false,
                        users:[],
                        errorMsg:error.message
                    })
                }
            )
        }
    }
}
</script>

List.vue(List组件)

<template>
  <div class="row">
    <div v-show="info.users.length" class="card" v-for="userObj in info.users" :key="userObj.id">
      <a :href="userObj.html_url" target="_blank">
        <img :src="userObj.avatar_url" style="width: 100px" />
      </a>
      <p class="card-text">{{userObj.login}}</p>
    </div>
    <!-- 显示欢迎词 -->
    <h2 v-show="info.isFirst">欢迎使用,输入关键词,点击按钮即可搜索</h2>

    <!-- 显示错误信息 -->
    <h2 v-show="info.errorMsg">{{info.errorMsg}}</h2>

    <!-- 显示加载中 -->
    <h2 v-show="info.isLoading">正在加载...</h2>

  </div>
</template>

<script>
export default {
  name: "List",
  props:["info"]
};
</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>

相关链接

github搜索案例(vue版本)之搭建静态页面
github搜索案例(vue版本)之展示数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值