vue资源承诺回调

时间:2016-04-30 20:51:01

标签: vue.js vue-resource

我想解析一个vue资源数据并根据我从服务器收到的数据发送回调请求,我将如何使用Vue.interceptors.then回调来实现这一点:

methods : function(){
var resource = this.$resource('index');
resource.save({name: 'jimmy'}).then(function (response) {
    //success callback
    //resend request lets say if response.data == 'test'

}, function (response) {
   // error callback
   console.log(response)
});
}

1 个答案:

答案 0 :(得分:3)

只需再次拨打电话,确保return由其创建的承诺:

methods: { someMethod: function(){
  var resource = this.$resource('index');
  resource.save({name: 'jimmy'})
    .then(function (response) {

      //resend request lets say if response.data == 'test'
      if (response.data === 'test') {
        // do request again and return the Promise.
        return resource.save({name: 'jimmy'})
      } else {
        return Promise.resolve(response)
      }
    })
    .then(function(response) {
      // do something with response
      // if there was a retry, `response` will be the second one.
    })
    .catch(function (error) {

    // catch() will catch any errors in the Promise chain, not just the first level.
      console.log(error)
    });
  }
}