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>