Which VueJS lifecycle hook must Asynchronous HTTP requests be called in?

TL;DR in the general (and safe) case, use created(). Vue’s initialization code is executed synchronously. Technically, any ASYNChronous code you run in beforeCreate(), created(), beforeMount() will only respond after all of those hooks finish. See demo: new Vue({ el: ‘#app’, beforeCreate() { setTimeout(() => { console.log(‘fastest asynchronous code ever’) }, 0); console.log(‘beforeCreate hook done’); }, … Read more

Is there any way to ‘watch’ for localstorage in Vuejs?

localStorage is not reactive but I needed to “watch” it because my app uses localstorage and didn’t want to re-write everything so here’s what I did using CustomEvent. I would dispatch a CustomEvent whenever you add something to storage localStorage.setItem(‘foo-key’, ‘data to store’) window.dispatchEvent(new CustomEvent(‘foo-key-localstorage-changed’, { detail: { storage: localStorage.getItem(‘foo-key’) } })); Then where ever … Read more