Geoff
Geoff

Reputation: 6629

Vuejs2 cancel http requests on route navigate

Am trying to cancel older uncomplete http requests when a user navigates away (destroyed) life cycle

currently i have a method

methods:{
   getdata(){
     this.$http.get("users").then((res)=>{

          ///do stuff with returned data
     })
   }
}

NOw how do i cancel the above http request if it isnt complete

SO something like this in the destroyed life cycle

destroyed(){
  //here cancel above http request
}

How do i go about this?

Upvotes: 0

Views: 385

Answers (1)

mtflud
mtflud

Reputation: 866

You could try something like this:

let ongoingRequest
export default {
    beforeDestroy() {
        ongoingRequest.abort()
    },
    methods: {
        getdata() {
            this.$http.get("users", {
                before(request) {
                    ongoingRequest = request
                }
            }).then((res) => {
                ///do stuff with returned data
            })
        }
    }
}

I found an issue talking about that here.

Upvotes: 2

Related Questions