前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >stroageState Vue 缓存hook

stroageState Vue 缓存hook

作者头像
copy_left
发布2020-04-09 11:48:56
3770
发布2020-04-09 11:48:56
举报
文章被收录于专栏:方球方球

chart.gif

使用例子
代码语言:javascript
复制
<template>
  <div class="home">

    <div>
      <Card style='margin: 20% auto; width: 600px' title=''>

        <p> {{ userInfo }} </p>
        <Input v-model="userName" />
        <Button @click='updateUserName' > update </Button>
        <Button @click='clearUserInfo' > clear </Button>
        <Button @click='addPrefix' > addPrefix </Button>
   
      </Card>
    </div>
    
  </div>
</template>

<script>
import { ref } from '@vue/composition-api'
import strongeStateHook from '@/hooks/strongeStateHook'


export default { 

  setup(){

    const [ userInfo, updateState ] = strongeStateHook(localStorage, 'USER_INFO', { name: 'coco' })
    const userName = ref('')
    const updateUserName = () => updateState({ ...userInfo.value, name: userName.value })
    const clearUserInfo = () => updateState()
    const addPrefix = () => updateState(userInfo => { return { name: `super-${userInfo.name}` } })

    return {
      userName,
      userInfo,
      updateUserName,
      clearUserInfo,
      addPrefix
    }
    

  }

}
</script>
实现
代码语言:javascript
复制
import { ref } from '@vue/composition-api'

function isFunction(obj){
    return typeof obj === 'function'
}

function getStoreValue(storage, key, defaultValue){

    const raw = storage.getItem(key)

    if(raw){
        return JSON.parse(raw)
    }

    if(isFunction(storage)){
        return defaultValue()
    }
    
    return defaultValue
}


/**
 * 
 * @param { localStorage | sessionStorage } storage  
 * @param { string } key 缓存字段名 
 * @param { any } defaultValue 默认数据
 * @returns { array } [ state, updateState ] 返回缓存ref对象,及更新方法。 更新数据为空时,清楚缓存字段 
 * @summary 缓存基础函数,封装解析及格式化操作
 * @example
 *  
 * const [ userInfo, updateState ] = strongeStateHook(localStorage, 'USER_INFO', { nickname: 'coco', age: 24 })
 *  
 * const closeUserInfo = () => updateState()
 * const updateNickname = name => updateState({ ...userInfo.value, nickname: name }) 
 * 
 */
export default function stroageState(storage, key, defaultValue){

    const state = ref( getStoreValue(storage, key, defaultValue) )
    
    function updateState(value){

        if(typeof value === 'undefined'){
            storage.removeItem(key)
            state.value = undefined
            return 
        }

        if(isFunction(value)){
            const previousState = getStoreValue(storage, key, defaultValue)
            const currentState = value(previousState)
            storage.setItem(key, JSON.stringify(currentState))
            state.value = currentState
            return 
        }

        storage.setItem(key, JSON.stringify(value))
        state.value = value
    }

    return [ state, updateState ]
    
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用例子
  • 实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档