DRY on Vue.js / Nuxt.js [beginner level]

Xyz Zyx
1 min readDec 18, 2019

--

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

  1. Save the loading variable somewhere, globally..let’s say store/index.js
  2. Have an interceptor for every web request (for nuxt.js -> “plugins/axios.js” and remember to register the plugin)
  3. $store.state.loading is now available for usage

check the gist below for an example with Nuxt & Axios

click for all

--

--

Xyz Zyx
Xyz Zyx

No responses yet