/**
* 本地localStorage、sessionStorage的设置、获取、移除
* @method storage
* @param isSession 是否是sessionStorage,默认为localStorage
* @return { set(key: String, value: Any): void , get(key: String): obj , clear(key: String): void }
*/
export const storage = function (isSession = false) {
const target = isSession ? sessionStorage : localStorage;
return {
set (key, value) {
target.setItem(key, escape(JSON.stringify(value)));
},
get (key) {
return JSON.parse(unescape(target.getItem(key)));
},
clear (key) {
target.clear(key);
}
};
};
session调用:
const session = storage(true);
session.set(“文件名”,"内容“)
session.get(“文件名”)
session.clear(“文件名”);
local调用:
const local= storage();
local.set(“文件名”,"内容“)
local.get(“文件名”)
local.clear(“文件名”);
用起来还是很方便的,另推荐使用数据管理持久化插件vuex-persist 数据状态统一管理 省去很多不必要的的操作