Reputation: 770
I created a global variable in main.js
like so :
Vue.prototype.$foo = []
new Vue({
router,
render: h => h(App)
}).$mount('#app')
Is there a way to cache this $foo
variable to access it on page refresh?
Upvotes: 0
Views: 701
Reputation: 138216
Data persistence can be done with the Web Storage API (including localStorage
) or IndexedDB API. localStorage
is probably the simplest for storing string values (e.g., stringified arrays/objects).
To restore the value, use JSON.parse()
on the localStorage
value:
Vue.prototype.$foo = localStorage.foo
? JSON.parse(localStorage.foo)
: {} /* default value */
To save the value, use JSON.stringify()
on the value to be stored:
localStorage.foo = JSON.stringify(/* new value */)
Upvotes: 1