axios-一个基于Promise的http库 可用在浏览器和node.js中

axios-一个基于Promise的http库 可用在浏览器和node.js中,第1张

引入axios 1、yarn add axios 在main.js 2、全局引入axios
import axios from 'axios'
3、加入基准地址
axios.defaults.baseURL='http://www.liulongbin.top:3006'
4、把axios挂载到vue原型上 以后vue实例(组件)都是可以直访问的
Vue.prototype.$axios=axios
示例: 一、axios查询所有图书信息

接口网址说明--ShowDochttps://www.showdoc.com.cn/ajaxapi/3753323218792173

    getBook() {
      this.$axios({
        method: "GET",
        url: "/api/getbooks",
      }).then((res) => {
        console.log(res);
      });
    },
async await方法
    async getBook() {
      let res = await this.$axios({
        method: "GET",
        url: "/api/getbooks",
      });
      console.log(res);
    },
二、axios根据书名获取图书信息
    getBookname(){
      this.$axios({
        method:'get',
        url:'/api/getbooks',
        params:{
          bookname:this.bookname
        }
      }).then(res=>{
        console.log(res);
      })
    },
    
    
bookname: "",
async await方法
    async getBookname() {
      let res = await this.$axios({
        method: "get",
        url: "/api/getbooks",
        params: {
          bookname: this.bookname,
        },
      })
      console.log(res);
    },
三、axios添加图书信息图书信息
    addBookname(){
      this.$axios({
        method:'POST',
        url:'/api/addbook',
        data:{
          bookname:this.bookname,
          author:this.author,
          publisher:this.publisher,
        }
      }).then(res=>{
        console.log(res);
      })
    },
    
    
    
    
bookname: "",
author:'',
publisher:''
async await方法
    async addBookname() {
      let res = await this.$axios({
        method: "POST",
        url: "/api/addbook",
        data: {
          bookname: this.bookname,
          author: this.author,
          publisher: this.publisher,
        },
      })
      console.log(res);
    },

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/993257.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存