关于 axios和vue-axios

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。
一、
安装:$ npm install axios
在入口文件main.js中引入
在原型链上绑定

import axios from 'axios'
Vue.prototype.$http = axios

在需调接口的vue组件中

   getHospitals () {
    
    
      this.$http`在这里插入代码片`.post('/list').then(response=>{
    
    
        console.log(response.data)
        this.age = response.data.age
      }).catch(err=>{
    
    
        console.log("获取错误")
      })
    }
就可以成功获取到数据了
加粗部分内容要一样$axios\$http\$ajax都可以 具体区别等我弄明白了再说

在这里插入图片描述
二、使用vue-axios
npm install --save axios vue-axios
安装vue-axio是和axios 可以在package.json文件中查看安装的版本

在入口文件main.js文件添加


import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)

在需调接口的vue组件中

  getHospitals () {
    
    
        this.axios.post('/list').then(response=>
          console.log(response.data)
          this.age = response.data.age
        }).catch(err=>{
    
    
          console.log("获取错误")
        })
      }

同样可以成功调用接口
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44227395/article/details/104840383