| 123456789101112131415161718 |
- import { onMounted, ref, watch } from "vue";
- export function memorizeVar<T>(key: string, defaultValue: T) {
- const variable = ref<T>(defaultValue);
- onMounted(() => {
- const v = localStorage.getItem(key);
- if (v)
- variable.value = JSON.parse(v);
- });
- watch(variable, (newValue) => {
- localStorage.setItem(key, JSON.stringify(newValue));
- });
- return {
- variable
- };
- }
|