Vue js拦截器仅返回响应的某些部分

时间:2018-03-19 14:23:54

标签: vue.js vue-resource

我以下列格式从服务器获取responses

{  
   "Success":true,
   "StatusCode":"000",
   "Result":[  
      {  
         "Id":"1",
         "Text":"Cash"
      },
      {  
         "Id":"1",
         "Text":"Check"
      },
      {  
         "Id":"3",
         "Text":"Bank"
      }
   ],
   "Error":null
}

当回复成功时。即"Success":true, 我想从interceptor仅返回响应的结果部分。

interceptor我有以下实施。

Vue.http.interceptors.push(function (request, next) {
  this.$Progress.start()
  next(function (response) {  
    if (!response.body.Success) {
      this.$toastr('error', response.body.Error.Message)
      return false;
    }
    else{
      this.$Progress.finish()
    }    
  })
})

当我向服务器发出请求时,我正在使用单独的服务从响应正文中获取信息。

目前我正在做的是。

  var promises = [];
  promises.push(ListService.getStateList());
  promises.push(ListService.getById("address/city/get/" + to.params.id));
  Promise.all(promises).then(response => {
    next(p => {
      p.StateCollection = p.getResultData(response[0].body, "array");
      p.vModel = p.getResultData(response[1].body, "singleobject");
    });
  });

getResultData

    getResultData(data, item) {
     if (data.Success) {
       return data.Result;
     } else {
       if (item == 'array'||item == 'list') {
        return [];
       } else {
         return {};
       }
     }
   },

是否可以仅返回interceptor本身的响应的结果部分?

我不想调用getResultData方法,因为我目前正在从响应中获取Result

我想直接将对象绑定到对象。

例如:p.vModel= response。无需从正文中获取信息。

0 个答案:

没有答案