Vue-resource

一、vue-resource插件的安装

  1.通过npm

   npm install vue-resource

  2.CDN

  <script src=https://cdn.jsdelivr.net/npm/vue-resource@1.5.1></script>

二、vue-resource用法

// global Vue object
Vue.http.get('/someUrl', [config]).then(successCallback, errorCallback);
Vue.http.post('/someUrl', [body], [config]).then(successCallback, errorCallback);

// in a Vue instance
this.$http.get('/someUrl', [config]).then(successCallback, errorCallback);
this.$http.post('/someUrl', [body], [config]).then(successCallback, errorCallback);

举个栗子

<script src=“https://cdn.jsdelivr.net/npm/vue-resource@1.5.1”></script>
<script>
var myapp = new Vue({
  el:"#app",
  data:{

  },
  methods:{
    getresource(){  
      //get请求--get(url, [config]))--,通过.then回调函数获得数据,返回的数据在result.body中, then 中第一个function表示成功的回调,第二个function表示失败的回调,失败的回调是可选的,可以不传
      this.$http.get("http-test.php").then(
      function(result){
        console.log(result.body)
      },function(err){
        console.log(err)
      })
    },
    postresource(){
      //post请求--post(url, [body], [config])-- 第二个参数是发送到服务端的数据不能省略---手动发起的post请求默认没有表单格式,所以有的服务起处理不了,通过设置第三个参数emulateJSON:true 是设置提交的内容类型为普通表单数据格式
      this.$http.post("http-test.php",{},{emulateJSON:true}).then(
      function(result){
        console.log(result.body)
      },function(err){
        console.log(err)
      })
    }
  }
})
</script>

更多姿势: https://github.com/pagekit/vue-resource

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值