vue get请求实例获取笑话

 写的一个简单的vue get请求

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试get实例(runoob.com)</title>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
</head>
<body>
<div id="app">
  <h1>笑话列表</h1>
  <ul >
	  <li v-for="item in josarr" :key="item.id">
		 {{ item }}
	  </li>
  </ul>
<button @click="send">点击获取笑话</button>
</div>
<style>
#app {width:800px; margin:0 auto ; background:#ccc}
ul li{ width:95%; background:#eee; margin:0 auto; padding-top:10px;}
</style>
<script type = "text/javascript">
new Vue({
  el: '#app',
    data: {
    josarr: []
  },
  methods: {
    send: function () { //点击的时候加载
     axios({
        method:'get',
        url:'https://autumnfish.cn/api/joke/list?num=3',
        /*params:{
          num:3 
        }*/
     }).then(resp=>{	 
      this.josarr=resp.data.jokes	//数据赋值   
     }).catch(resp =>{
      console.log('failure');
     })
    }
  },
  mounted:function(){
    this.send(); //生命周期中  挂载完成的时候加载  也可以是created()看不同需求
  }
})
</script>
</body>
</html>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值