Skip to content

Commit e506b6b

Browse files
committed
修复工作区中标签页无法删除的bug
1 parent 4cc9a07 commit e506b6b

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

src/common.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import localforage from "localforage";
2+
13
export const TabItem = function () {
24
this.id = "";
35
this.title = "";
@@ -27,3 +29,30 @@ export const getQueryVariable = (variable) => {
2729
}
2830
return ''
2931
}
32+
33+
/**
34+
* 保存数据
35+
* @param key 键
36+
* @param data 值
37+
* @param serialization 是否序列化
38+
*/
39+
export const saveData = async (key, data, serialization = true) => {
40+
console.debug("saveData key:" , key + " data:" , data)
41+
if (serialization) {
42+
data = JSON.stringify(data)
43+
}
44+
await localforage.setItem(key, data)
45+
}
46+
47+
export const getData = (key, serialization = true) => {
48+
return new Promise((resolve, reject) => {
49+
localforage.getItem(key).then((data) => {
50+
if (serialization) {
51+
data = JSON.parse(data)
52+
}
53+
resolve(data)
54+
}).catch((err) => {
55+
reject(err)
56+
})
57+
})
58+
}

src/views/Main.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191

9292
<script setup>
9393
import {ref, onMounted} from 'vue'
94-
import {TabItem, WorkSpaceItem} from "../common.js";
94+
import {getData, saveData, TabItem, WorkSpaceItem} from "../common.js";
9595
import {ElNotification} from "element-plus";
9696
import localforage from "localforage";
9797
@@ -116,7 +116,7 @@ const createWorkSpace = async () => {
116116
workSpaceItem.workSpaceName = workspaceName.value
117117
workSpaceItem.fid = new Date().getTime()
118118
workSpaceItem.saveDataTime = new Date().toLocaleString()
119-
const res = await localforage.setItem(workSpaceItem.fid, workSpaceItem)
119+
await saveData(workSpaceItem.fid, workSpaceItem)
120120
// 清空工作区名称
121121
workspaceName.value = ''
122122
// 刷新工作区列表
@@ -133,7 +133,7 @@ const loadWorkSpaces = async () => {
133133
const sortedObjKeys = res.sort();
134134
for (let index in sortedObjKeys) {
135135
const fid = sortedObjKeys[index]
136-
const workSpaceItem = await localforage.getItem(fid)
136+
const workSpaceItem = await getData(fid)
137137
workSpaceList.value.push(workSpaceItem)
138138
}
139139
}
@@ -176,7 +176,7 @@ const closeAllTabFun = () => {
176176
*/
177177
const savePages = async (fid) => {
178178
// 查询当前工作区
179-
const workspaceItem = await localforage.getItem(fid)
179+
const workspaceItem = await getData(fid)
180180
if (workspaceItem === null) {
181181
ElNotification({
182182
message: '工作区不存在',
@@ -199,7 +199,7 @@ const savePages = async (fid) => {
199199
workspaceItem.spaceTabs.push(tabItem)
200200
})
201201
// 保存工作区
202-
const res = await localforage.setItem(workspaceItem.fid, workspaceItem)
202+
await saveData(workspaceItem.fid, workspaceItem)
203203
ElNotification({
204204
message: '保存成功',
205205
type: 'success',
@@ -215,7 +215,7 @@ const savePages = async (fid) => {
215215
*/
216216
const handoff = async (fid) => {
217217
// 查询当前工作区
218-
const workspaceItem = await localforage.getItem(fid)
218+
const workspaceItem = await getData(fid)
219219
if (workspaceItem === null) {
220220
ElNotification({
221221
message: '工作区不存在',
@@ -263,7 +263,7 @@ const openWorkspace = () => {
263263
*/
264264
const deleteWorkspace = async (fid) => {
265265
// 查询当前工作区
266-
const workspaceItem = await localforage.getItem(fid)
266+
const workspaceItem = await getData(fid)
267267
if (workspaceItem === null) {
268268
ElNotification({
269269
message: '工作区不存在',

src/views/workspaceManager.vue

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767
<script setup>
6868
import {onMounted, ref} from "vue";
69-
import {getQueryVariable} from "../common.js";
69+
import {getData, getQueryVariable, saveData} from "../common.js";
7070
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
7171
import {ElNotification} from "element-plus";
7272
import localforage from "localforage";
@@ -93,7 +93,7 @@ onMounted(async () => {
9393
9494
const loadPageData = async () => {
9595
// 从数据库中获取工作区信息
96-
const item = await localforage.getItem(fid.value)
96+
const item = await getData(fid.value)
9797
if (item === null) {
9898
ElNotification({
9999
message: '工作区不存在',
@@ -118,7 +118,7 @@ const view = (pageUrl) => {
118118
);
119119
}
120120
121-
const deletePage = (pageId) => {
121+
const deletePage = async (pageId) => {
122122
// 遍历工作区中的页面,删除id为pageId的页面
123123
for (let i = 0; i < workspaceItem.value.spaceTabs.length; i++) {
124124
const pageItem = workspaceItem.value.spaceTabs[i];
@@ -127,8 +127,9 @@ const deletePage = (pageId) => {
127127
break
128128
}
129129
}
130-
// 更新数据库中的工作区信息
131-
localforage.setItem(fid.value, workspaceItem.value).then(() => {
130+
console.debug(workspaceItem.value)
131+
// 更新本地存储
132+
saveData(fid.value, workspaceItem.value).then(() => {
132133
ElNotification({
133134
message: '删除成功',
134135
type: 'success',

0 commit comments

Comments
 (0)