Vue function that provides way to read, update and delete a localStorage key
interface StorageOptions {
isParsing: boolean
serializer?: SerializerFunction
deserializer?: DeserializerFunction
}
function useLocalStorage(
key: string,
options?: StorageOptions,
runOnMount?: boolean
): {
item: Ref<any>
getItem: () => void
setItem: (newVal: any) => void
removeItem: () => void
}
key: string
the localStorage key you wish to get/set/removeoptions: StorageOptions
isParsing: boolean
whether to enable parsing the localStorage key value or not,false
by defaultserializer: SerializerFunction
a custom serializer,JSON.stringify
by defaultdeserializer: DeserializerFunction
a custom deserializer,JSON.parse
by default
runOnMount: boolean
whether to get the localStorage key on mount or not,true
by default
item: Ref<any>
the localStorage key value, it can be null, a string or a JSON object/arraygetItem: Function
get the localStorage key valuesetItem: Function
set the localStorage key valuenewVal: any
: the value to set, can be a string or an object/array
removeItem: Function
delete the localStorage key
<template>
<div>
<div>Item: {{ item }}</div>
<button @click="getItem">Get item</button>
<button @click="setItem('Value here')">Set item</button>
<button @click="removeItem">Remove item</button>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
import { useLocalStorage } from 'vue-use-kit'
export default Vue.extend({
name: 'UseLocalStorageDemo',
setup() {
const { item, getItem, setItem, removeItem } = useLocalStorage(
'i_love_local_storage'
)
return {
item,
getItem,
setItem,
removeItem
}
}
})
</script>