Search.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. import axios from 'axios';
  16. export default {
  17. name: "Search",
  18. data(){
  19. return{
  20. keyWord:''
  21. }
  22. },
  23. methods:{
  24. searchUsers(){
  25. // 请求前更新List的数据
  26. this.$bus.$emit('updataListData',{isFirst:false,isLoading:true,errMsg:'',users:[]})
  27. axios.get(`https://api.github.com/search/users?q=${this.keyWord}`).then(
  28. response => {
  29. // console.log('请求成功了',response.data);
  30. // console.log('请求成功了',response.data.items);
  31. // 请求成功后更新List的数据
  32. this.$bus.$emit('updataListData',{isLoading:false,errMsg:'',users:response.data.items})
  33. },
  34. error => {
  35. // console.log('请求失败了',error.message);
  36. // 请求失败后更新List的数据
  37. this.$bus.$emit('updataListData',{isLoading:false,errMsg:error.message,users:[]})
  38. }
  39. )
  40. }
  41. }
  42. };
  43. </script>
  44. <style>
  45. </style>