Skip to content

Commit 6fb6871

Browse files
committed
fix: 修复编辑无法修改链接的问题
1 parent d9e7dfb commit 6fb6871

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

front/src/components/LinkDialog.vue

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,12 @@ const props = defineProps<{
122122
show: boolean
123123
mode: 'add' | 'update'
124124
link?: Link
125-
categories: string[] // 新增 categories prop
125+
categories: string[]
126126
}>()
127127
128128
const emit = defineEmits<{
129129
(e: 'update:show', value: boolean): void
130-
(e: 'submit', value: Link): void
130+
(e: 'submit', value: any): void
131131
}>()
132132
133133
const query = ref('')
@@ -141,6 +141,25 @@ const formData = ref({
141141
sortIndex: 0,
142142
})
143143
144+
const oldUrl = ref<string | undefined>('')
145+
146+
// 当 link 属性改变时更新表单数据
147+
watch(() => props.link, (newLink) => {
148+
if (newLink) {
149+
formData.value = { ...newLink }
150+
oldUrl.value = newLink.url
151+
} else {
152+
formData.value = {
153+
name: '',
154+
url: '',
155+
icon: '',
156+
category: '',
157+
sortIndex: 0,
158+
}
159+
oldUrl.value = ''
160+
}
161+
}, { immediate: true })
162+
144163
// 处理分类输入
145164
const handleCategoryInput = (event: Event) => {
146165
const value = (event.target as HTMLInputElement).value
@@ -158,20 +177,6 @@ const filteredCategories = computed(() => {
158177
)
159178
})
160179
161-
// 当 link 属性改变时更新表单数据
162-
watch(() => props.link, (newLink) => {
163-
if (newLink) {
164-
formData.value = { ...newLink }
165-
} else {
166-
formData.value = {
167-
name: '',
168-
url: '',
169-
icon: '',
170-
category: '',
171-
sortIndex: 0,
172-
}
173-
}
174-
}, { immediate: true })
175180
176181
// 获取图标
177182
const fetchIcon = async () => {
@@ -193,7 +198,11 @@ const fetchIcon = async () => {
193198
194199
// 提交表单
195200
const handleSubmit = () => {
196-
emit('submit', { ...formData.value })
201+
if (props.mode === 'update') {
202+
emit('submit', { link: { ...formData.value }, oldUrl: oldUrl.value })
203+
} else {
204+
emit('submit', { link: { ...formData.value } })
205+
}
197206
emit('update:show', false)
198207
}
199208
</script>

front/src/views/Nav.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ const closeAddDialog = () => {
219219
isAddDialogOpen.value = false
220220
}
221221
222-
const handleAdd = async (link: Link) => {
222+
const handleAdd = async (payload: { link: Link }) => {
223+
const { link } = payload
223224
try {
224225
await api.addLink(link)
225226
await fetchLinks()
@@ -238,9 +239,11 @@ const closeUpdateDialog = () => {
238239
isUpdateDialogOpen.value = false
239240
}
240241
241-
const handleUpdate = async (link: Link) => {
242+
const handleUpdate = async (payload: { link: Link, oldUrl: string }) => {
243+
const { link, oldUrl: prevUrl } = payload
242244
try {
243-
await api.updateLink(store.links.findIndex((l: Link) => l.url === link.url), link)
245+
const index = store.links.findIndex((l: Link) => l.url === prevUrl)
246+
await api.updateLink(index, link)
244247
await fetchLinks()
245248
closeUpdateDialog()
246249
} catch (error) {

0 commit comments

Comments
 (0)