(37) Two Ajax libraries commonly used in vue projects

Previous: (36) Vue solves Ajax cross-domain problems

First look at a github search case.
There is a search box, enter the relevant user name, you can fuzzy search out the user, and display it below
Please add a picture description

insert image description here
Step 1: We used the third-party style library bootstrap. First, we need to see a css folder in the public folder, put it into the style library, and then import it on the index.html page
Please add a picture description

    <!-- 引入第三方样式 -->
    <link rel="stylesheet" href="<%=BASE_URL%>css/bootstrap.css">

Step 2: Split the components, search and text box as a component Search, and display users as a component List

axios implementation

General Ajax request library, officially recommended, widely used
axios installation command: npm i axios
App component

<template>
  <div class="container">
    <Search/>
    <List/>
  </div>
</template>
<script>
import Search from "@/components/Search";
import List from "@/components/List";
export default {
    
    
  name: "App",
  components: {
    
    List, Search},
}
</script>
<style>
</style>

Seach component

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

<script>

import axios from "axios";

export default {
    
    
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Search",
  data(){
    
    
    return{
    
    
      keyWord:''
    }
  },
  methods:{
    
    
    searchUsers(){
    
    
      if (this.keyWord === ""){
    
    
        alert("输入不能为空")
        return
      }
      //请求前更新List的数据
      this.$bus.$emit('updateListData',{
    
    isFirst:false,isLoading:true,errMsg:'',users:[]})
      axios.get(`https://api.github.com/search/users?q=${
    
    this.keyWord}`).then(
          response => {
    
    
            //请求成功后更新List的数据
            this.$bus.$emit('updateListData',{
    
    isLoading:false,errMsg:'',users:response.data.items})
          },
          error => {
    
    
            console.log('请求失败',error.message)
            //请求后更新List的数据
            this.$bus.$emit('updateListData',{
    
    isLoading:false,errMsg:error.message,users:[]})
          }
      )
    }
  }
}
</script>
<style scoped>
</style>

List component

<template>
  <div class="row">
  <!--展示用户列表-->
    <div v-show="info.users.length" class="card" v-for="user in info.users" :key="user.login">
      <a :href="user.html_url" target="_blank">
        <img :src="user.avatar_url" style='width: 100px'/>
      </a>
      <p class="card-text">{
    
    {
    
    user.login}}</p>
    </div>
    <!--展示欢迎词-->
    <h2 v-show="info.isFirst">欢迎使用</h2>
    <!--展示加载中-->
    <h2 v-show="info.isLoading">加载中....</h2>
    <!--展示错误信息-->
    <h2 v-show="info.errMsg">{
    
    {
    
    info.errMsg}}</h2>
  </div>
</template>

<script>
export default {
    
    
  // eslint-disable-next-line vue/multi-word-component-names
  name: "List",
  data(){
    
    
    return{
    
    
      info: {
    
    
        isFirst:true,
        isLoading:false,
        errMsg:'',
        users:[]
      }
    }
  },
  mounted() {
    
    
    this.$bus.$on('updateListData',(dataObj)=>{
    
    
      this.info = {
    
    ...this.info,...dataObj}
    })
  }
}
</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>

vue-resource implementation

vue-resource: It is a plug-in library that encapsulates ajax, and its usage is similar to that of axios.
vue-resource installation command: npm i vue-resource
After using this plugin, there are more Vue and VueComponent $http, through $http.getor $http.postAjax to request
the vue plugin library, vue1.x is widely used, and the official is no longer maintained .
After installation, you need to import it in mian.js

import vueResource from "vue-resource";
Vue.use(vueResource)

Please add a picture description
The Seach component is changed to:

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

<script>

import axios from "axios";

export default {
    
    
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Search",
  data(){
    
    
    return{
    
    
      keyWord:''
    }
  },
  methods:{
    
    
    searchUsers(){
    
    
      if (this.keyWord === ""){
    
    
        alert("输入不能为空")
        return
      }
      //请求前更新List的数据
      this.$bus.$emit('updateListData',{
    
    isFirst:false,isLoading:true,errMsg:'',users:[]})
      this.$http.get(`https://api.github.com/search/users?q=${
    
    this.keyWord}`).then(
          response => {
    
    
            //请求成功后更新List的数据
            this.$bus.$emit('updateListData',{
    
    isLoading:false,errMsg:'',users:response.data.items})
          },
          error => {
    
    
            console.log('请求失败',error.message)
            //请求后更新List的数据
            this.$bus.$emit('updateListData',{
    
    isLoading:false,errMsg:error.message,users:[]})
          }
      )
    }
  }
}
</script>

<style scoped>

</style>

Guess you like

Origin blog.csdn.net/weixin_45832694/article/details/129130513