From dca87eea7ee73a25f3193dbe154bdfce9e4e5a35 Mon Sep 17 00:00:00 2001 From: lyt-Top <1105290566@qq.com> Date: Thu, 27 May 2021 21:01:41 +0800 Subject: [PATCH] =?UTF-8?q?'admin-21.05.27:=E6=96=B0=E5=A2=9Eutils?= =?UTF-8?q?=E4=B8=8B=E7=9A=84storage.ts=E8=AF=BB=E5=8F=96=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E7=BC=93=E5=AD=98=E6=96=B0=E5=86=99=E6=B3=95?= =?UTF-8?q?=EF=BC=8C=E5=BB=BA=E8=AE=AE=E4=BD=BF=E7=94=A8'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 ++-- src/utils/storage.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 8defba4..d1664f4 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "lint-fix": "eslint --fix --ext .js --ext .jsx --ext .vue src/" }, "dependencies": { - "@antv/g6": "^4.3.0", + "@antv/g6": "^4.3.2", "axios": "^0.21.1", "clipboard": "^2.0.8", "countup.js": "^2.0.7", @@ -46,7 +46,7 @@ "prettier": "^2.3.0", "sass": "^1.34.0", "sass-loader": "^11.1.1", - "typescript": "^4.2.4", + "typescript": "^4.3.2", "vite": "^2.3.4", "vue-eslint-parser": "^7.6.0" }, diff --git a/src/utils/storage.ts b/src/utils/storage.ts index 5d7f1aa..e670ee7 100644 --- a/src/utils/storage.ts +++ b/src/utils/storage.ts @@ -35,3 +35,46 @@ export function removeSession(key: string) { export function clearSession() { window.sessionStorage.clear(); } + +// 新写法,简单易记,建议使用 +// 1、window.localStorage 浏览器永久缓存 +export const Local = { + // 设置永久缓存 + set(key: string, val: any) { + window.localStorage.setItem(key, JSON.stringify(val)); + }, + // 获取永久缓存 + get(key: string) { + let json: any = window.localStorage.getItem(key); + return JSON.parse(json); + }, + // 移除永久缓存 + remove(key: string) { + window.localStorage.removeItem(key); + }, + // 移除全部永久缓存 + clear() { + window.localStorage.clear(); + }, +}; + +// 2、window.sessionStorage 浏览器临时缓存 +export const Session = { + // 设置临时缓存 + set(key: string, val: any) { + window.sessionStorage.setItem(key, JSON.stringify(val)); + }, + // 获取临时缓存 + get(key: string) { + let json: any = window.sessionStorage.getItem(key); + return JSON.parse(json); + }, + // 移除临时缓存 + removeLocal(key: string) { + window.sessionStorage.removeItem(key); + }, + // 移除全部临时缓存 + clear() { + window.sessionStorage.clear(); + }, +};