【Vue2.0】—github小案例(二十三)

【Vue2.0】—github小案例(二十三)

在这里插入图片描述

<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="sendData">Search</button>
    </div>
  </section>

</template>

<script>
  import axios from 'axios'
  export default {
    name: 'Search',
    data() {
      return {
        keyWord: ''
      }
    },
    methods: {
      sendData() {
        //请求前更新List里面的数据
        this.$bus.$emit('updateListData', {
          isLoading: true,
          errMsg: '',
          users: [],
          isFirst: false
        })
        axios.get(`https://api.github.com/search/users?q=${this.keyWord}`).then(
          response => {
            console.log('请求成功');
            //请求成功后更新数据
            this.$bus.$emit('updateListData', {
              isLoading: false,
              errMsg: '',
              users: response.data.items
            })
          },
          error => {
            //请求失败后更新数据
            this.$bus.$emit('updateListData', {
              isLoading: false,
              errMsg: error.message,
              users: []
            })

          }
        )
      }
    }
  }

</script>

<style>

</style>

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>