vue-resource timeout如何设置回调

我的项目中引入的是vue2.min.js 、 vue-resource.min.js 文件,这个我要如何设置回调函数呢?

      this.$http.post(url, {
        keywords : [
          { 'key' : key, 'limit' : 50 }
        ]
      },{timeout : 3000}).then(function(res) {
          console.log(res)
      },function(err){
          console.log(err)
      })

       
阅读 4.8k
1 个回答
找到解决方案了,给遇到同样问题的小伙伴一个参考!
使用拦截器
Vue.http.interceptors.push(function(request, next) {
    var timeout
    if (request.timeout) {
      clearTimeout(timeout)
      timeout = setTimeout(function() {
        console.log('请求超时啦。。。。')
        request.abort()
      }.bind(this), request.timeout)
    }
    next(function(response) {
      return response
    })
  })
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进