美文网首页IT@程序员猿媛
vue-resource中的Promise对象

vue-resource中的Promise对象

作者: 勿念及时雨 | 来源:发表于2019-04-21 23:42 被阅读1次

    vue-resource基本HTTP调用和RESTful调用action方法执行后都会返回一个Promise对象,该Promise对象提供了then、catch、finally等常用方法来注册回调函数。代码示例如下:

    var promise=this.$http.post(
      'http://example.com/book/create',
      //请求体中要发送给服务端的数据
      {
        cat:'1',
        name:'network'
      },
      {
        'headers':{
          'Content-Type':'x-www-form-urlencoded'
        }
      }
    )
    promise.then(function(response){
      //成功回调
      console.log(response.data)
    },function(response){
      //失败回调
      console.log('something wrong')
    })
    promise.catch(function(response){
      //失败回调
      console.log('something wrong')
    })
    promise.finally(function(){
      //执行完成功或者失败回调后都会执行此逻辑
    })
    

    注:所有回调函数的this都指向组件实例。

    相关文章

      网友评论

        本文标题:vue-resource中的Promise对象

        本文链接:https://www.haomeiwen.com/subject/bvosgqtx.html