为【storage】提供过期时间,get,set等函数
type useStorageOption = {
keyBase?: string
expires?: number
}
function useStorage(storage: Storage, option?: useStorageOption): {
get(key: string): any
set(key: string, value: any, expi?: number): void
remove(key: string): void
}
- storage — 供使用的storage(WindowLocalStorage或WindowSessionStorage)
- option — storage使用参数
- keyBase — storage中存储键的前缀,防止重复添加
- expires — storage中存储的默认过期时间,单位为秒,默认为0
- get(key) — 获取某个键对应的值,若失效则返回null
- set(key,value,expi) — 为某个键设置值及失效时间
- remove(key) — 移除某个键对应的值
import { useStorage} from 'chooks'
export default {
setup(){
const { get, set, remove } = useStorage({
keyBase: "test",
expires: 10000
})
set("a", 1, 10)
get("a")
remove("a")
return {}
}
}