封装localStorage

日结博客 18.04.18 HangZhou

对于一次次从 window对象里面拿localStorage这种行为简直是深恶痛绝

以下是我自己简单封装了一下storage

在项目文件夹下新建一个storage.js

get方法,传入key 和storage(可选,默认localStorage)


export const get = (key, storage = localStorage) => JSON.parse(storage.getItem(key))

set方法,传入key、value和storage(可选,默认localStorage)


export const set = (key, val, storage = localStorage) => storage.setItem(key, JSON.stringify(val))

remove方法,传入key和storage(可选,默认localStorage)

export const remove = (key, storage = localStorage) => storage.removeItem(key)

clear方法,传入storage(可选,默认localStorage)

export const clear = (storage = localStorage) => storage.clear()

今天偷个小懒,看了一天的文档,嘻嘻。

喜欢就点个赞吧。谢谢你~

:D

你可能感兴趣的:(前端)