Vue Get请求数据

VUE的使用之前只是看了数据绑定、筛选这些基本的功能,没有深入接触。接下来的一个项目会用到VUE来开发,所以又简单看看,热热身~

 

 

 

使用http请求需要用到vue-resource 【 https://github.com/vuejs/vue-resource 】

<script src="plugin/vue/vue.js"></script>
<script src="plugin/vue-resource/dist/vue-resource.js"></script>

写的一个非常简单的数据返回:

<?php
header('Content-type:text/json');
$str = '[{"id":"1","name":"penghui1","age":"20","phone":"15839922830"},
         {"id":"2","name":"penghui2","age":"21","phone":"15839922830"},
         {"id":"3","name":"penghui3","age":"22","phone":"15839922830"},
         {"id":"4","name":"penghui4","age":"23","phone":"15839922830"}]';
echo $str;
 ?>

返回结果如下:

114808_HRAH_3172968.png

前戏说完,步入正题,先贴一下效果:

当点击复原按钮进行请求数据 (绑定了get方法):

115345_MD9g_3172968.png

-------------------------分割线-------------------------

贴代码:

<body>
  <div id="app" class="container">

<button class="btn btn-primary" v-on:click="get">复原</button>
<table class="table table-condensed table-hover table-bordered">
      <tr class="success">
        <th>序号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>手机号</th>
        <th>操作</th>
      </tr>
      <tbody>
        <tr v-for="(user,index) in users">
          <td>{{index}}</td>
          <td>{{user.name}}</td>
          <td>{{user.age}}</td>
          <td>{{user.phone}}</td>
          <td>
            <div class="btn-group">
              <button class="btn btn-primary" v-on:click="edit(user,index)">编辑</button>
              <button class="btn btn-danger" v-on:click="deleted(index)">删除</button>
            </div>
          </td>
        </tr>
      </tbody>
    </table>
</div>
<script src="plugin/vue/vue.js"></script>
<script src="plugin/vue-resource/dist/vue-resource.js"></script>
<script>
new Vue({
el: "#app",
data: {
 api:'http://localhost:87/api/student.php',
 users: [],
},
methods: {
  get: function() {
    this.$http.get(this.api,{emulateJSON:true})
    .then( (response)=>{
      console.log(response.data);
      this.users = response.body;
    })
    .catch(function(response) {
      console.log(response);
    })
  }
},
});

</script>
</body>

 

get请求之前做过移动端开发,所以这次只是简单使用了一下get方式请求.

在获取到数据时要赋值给模型(this.users),在view上已经进行了数据模型的绑定,所以当this.users更新了数据值之后,页面就加载出来数据了。在http请求时会有很多参数,具体可以参考:

http://www.doc00.com/doc/1001004eg  比较详细

 

记:还有一些其他的功能操作就不往上写了,实现了添加、删除和编辑功能,代码却比较乱。就先记录一下网络请求和加载吧

转载于:https://my.oschina.net/u/3172968/blog/869849

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值