This commit is contained in:
SunWuyuan
2025-03-15 10:05:35 +08:00
parent c6b6bd2ce5
commit 4870eaefc8
+29 -19
View File
@@ -256,12 +256,8 @@ export default {
}, },
mounted() { mounted() {
// 使用 nextTick 确保在 DOM 更新后初始化
this.$nextTick(() => { this.$nextTick(() => {
this.savedState = { this.initializeSavedState()
list: [...this.modelValue.list],
text: this.modelValue.text
}
}) })
}, },
@@ -305,9 +301,10 @@ export default {
methods: { methods: {
// 初始化方法 // 初始化方法
initializeSavedState() { initializeSavedState() {
const currentList = this.modelValue.list || []
this.savedState = { this.savedState = {
list: [...(this.modelValue.list.length ? this.modelValue.list : this.originalList)], list: [...currentList],
text: this.modelValue.text || (this.originalList || []).join('\n') text: currentList.join('\n')
} }
}, },
@@ -337,11 +334,11 @@ export default {
// 更新保存状态 // 更新保存状态
updateSavedState() { updateSavedState() {
const currentList = this.modelValue.list || []
this.savedState = { this.savedState = {
list: [...this.modelValue.list], list: [...currentList],
text: this.modelValue.text text: currentList.join('\n')
}; }
this.$forceUpdate(); // 强制更新视图
}, },
// 学生管理方法 // 学生管理方法
@@ -417,8 +414,8 @@ export default {
// 保存和加载处理 // 保存和加载处理
async handleSave() { async handleSave() {
try { try {
await this.$emit('save'); await this.$emit('save')
this.updateSavedState(); this.updateSavedState()
} catch (error) { } catch (error) {
console.error('保存失败:', error); console.error('保存失败:', error);
throw error; throw error;
@@ -439,14 +436,27 @@ export default {
// 重写变更检测逻辑 // 重写变更检测逻辑
isStateChanged() { isStateChanged() {
if (!this.savedState) return false; if (!this.savedState) return false
const currentState = { const currentList = this.modelValue.list || []
list: this.modelValue.list, const savedList = this.savedState.list || []
text: this.modelValue.text
};
return JSON.stringify(currentState) !== JSON.stringify(this.savedState); // 比较列表长度
if (currentList.length !== savedList.length) return true
// 逐个比较列表项
for (let i = 0; i < currentList.length; i++) {
if (currentList[i] !== savedList[i]) return true
}
// 如果在高级模式下,还需要比较文本
if (this.modelValue.advanced) {
const currentText = this.modelValue.text || ''
const savedText = this.savedState.text || ''
if (currentText !== savedText) return true
}
return false
} }
} }
} }