vue-爬坑之路二 (路由切换时取消之前的正在进行的请求)

main.js 

Vue.Cancel = [] //全局定义一个对象

router.beforeEach((to, from, next) => {

while (Vue.Cancel.length > 0) { // 存储的对象进行abort()

Vue.Cancel.shift().abort()

}

}

请求的地方加上

 oneRequest = context.$http.post(url, newParams, //这里用的是vue-resource
            { // 发送请求前把request存放在Vue.Cancel对象中,为后面路由切换的时候中断使用
              // use before callback
              before (request) {
                Vue.Cancel.push(request)
              }
            }
          )

猜你喜欢

转载自blog.csdn.net/zhaofeiweb/article/details/86493801