DRY stand for “Don’t Repeat Yourself,” a basic principle of software development aimed at reducing repetition of information.
have you ever encounters this
You have a progress bar, or button that shows a loading indicator
start web request -> set is loading variable to true -> axios ends/error -> set is loading variable to false
…. repeat for every request
Well this is repetitive code, and here’s how you can easily remove in 3 easy steps
- Save the loading variable somewhere, globally..let’s say store/index.js
- Have an interceptor for every web request (for nuxt.js -> “plugins/axios.js” and remember to register the plugin)
- $store.state.loading is now available for usage
check the gist below for an example with Nuxt & Axios