1.vue-Resource

mian.js
image.png

this.$http.get

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

2.axios

import axios from ‘axios’

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

3.xhr

4.jq