Search.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <template>
  2. <section class="jumbotron">
  3. <h3 class="jumbotron-heading">Search Github Users</h3>
  4. <div>
  5. <input
  6. type="text"
  7. placeholder="enter the name you search"
  8. v-model="keyWord"
  9. />&nbsp;
  10. <button @click="searchUsers">Search</button>
  11. </div>
  12. </section>
  13. </template>
  14. <script>
  15. export default {
  16. name: "Search",
  17. data(){
  18. return{
  19. keyWord:''
  20. }
  21. },
  22. methods:{
  23. searchUsers(){
  24. // 请求前更新List的数据
  25. this.$bus.$emit('updataListData',{isFirst:false,isLoading:true,errMsg:'',users:[]})
  26. this.$http.get(`https://api.github.com/search/users?q=${this.keyWord}`).then(
  27. response => {
  28. // console.log('请求成功了',response.data);
  29. // console.log('请求成功了',response.data.items);
  30. // 请求成功后更新List的数据
  31. this.$bus.$emit('updataListData',{isLoading:false,errMsg:'',users:response.data.items})
  32. },
  33. error => {
  34. // console.log('请求失败了',error.message);
  35. // 请求失败后更新List的数据
  36. this.$bus.$emit('updataListData',{isLoading:false,errMsg:error.message,users:[]})
  37. }
  38. )
  39. }
  40. }
  41. };
  42. </script>
  43. <style>
  44. </style>