vue 资源承诺回调

vue resource promise callback

我想解析一个 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)
});
}

只需再次调用并确保您 return 它创建的 Promise:

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)
    });
  }
}