axios基本使用

ajax不是一门语言,而是一种访问方式,异步的js和xhml。
<script>标签引入axios,添加@click事件以后,根据输入的需要查询的信息条数,查询笑话结果。然后v-for显示到页面。
引用方法简单,axios.get().then()为基础,在连接中加入动态的需要查询的条数。
值得注意的是,在axios下,由于this指针不再是vue实例中的指针,如果在axios中使用this指针,指向的不一样,因此把vue实例中的this复制给that,让that和this指向同一个地址,然后在axios中使用that就可以访问

学习自B站黑马程序员教程,接口亦是,可访问

<!DOCTYPE html>
 
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../JS/vue.js"></script>
     <script src="https://unpkg.com/axios@0.21.1/dist/axios.min.js"></script>
</head>
<body>
  
    <div id="music">
          <button v-on:click="searchsongs" >更新笑话</button> 
          <input type="number" name="piece" id="num" v-model="num">
          <table border="1px" style="width: 50%; margin: auto;">
              <tr>
                  <td style="background-color: skyblue;">笑话</td>
                  
                
              </tr>
                <tr v-for="item in joke">

                <td>{{item}}</td>
          
                </tr>
          </table>
    </div>

</body>
   
<SCRipt>
    var search = new Vue({
        el: '#music',
        data:{
            songname:'',
            joke: [],
            num: 3,
        },
        methods: {
            searchsongs : function(){
                that=this;
                console.log(this.num)
              axios.get("https://autumnfish.cn/api/joke/list?num="+this.num)
                .then(function(response){ 
                    console.log(response);
                    that.joke=response.data.jokes;
                })
            }
        }
    })
</SCRipt>
 

</script>
 
</html>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值