I'm looking for a simple solution for the following problem:
I have a Vue component button with which I can make an ajax request. I would like to disable this button while the request is pending (to prevent multiple requests).
I'm looking for a simple solution for the following problem:
I have a Vue component button with which I can make an ajax request. I would like to disable this button while the request is pending (to prevent multiple requests).
Sounds like you want your action to set (commit) a flag when it starts and then clear it when it ends.
Try something like this in Vuex...
Now in your component, you can map the
loading
state and use it to disable your button, egAlternatively, you can maintain the progress of the request within your component if your action returns a promise (as above). For example, say your button has
and
I found another solution: It automatically generates a vuex module for the registered endpoints, watch those endpoints via axios interceptors and sets the proper 'pending' state values based on the status of the related requests.
https://github.com/pharkasbence/pending-request-tracker
State.js:
Component.vue: