it depends on the scenario in which you want the value to persist.
if only your component is reloading (rerendering) e.g by v-for
of v-if
or by router
you can use keep-alive to retain the value. similarly vuex store
can also be used.
if you want the value to persist even after reloading entire page, you should use localStorage
to store the value.
you can use computed getter and setter to change the value in localStorage like this
computed: {
value: {
get: () => localStorage.getItem('myCat') || 'defaultvalue';
set: (v) => localStorage.setItem('myCat', v);
},
},
of course you need to study the localstorage api for pro and cons as localstorage is not reactive.
on the other hand you want the value to be stored on server side then theirs a lot information to be considered, but you can use rest api to get and post value.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…