From 8295cefbb58f089aa266b5934492b86c1df757ae Mon Sep 17 00:00:00 2001 From: sunfree <17315650350@163.com> Date: Wed, 26 Jun 2024 21:09:48 +0800 Subject: [PATCH] add new --- src/stores/index.ts | 22 ++++++++++++++----- .../classticmanage/ClassticManageView.vue | 12 +++++----- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/stores/index.ts b/src/stores/index.ts index b1679f9..4f4fbc4 100644 --- a/src/stores/index.ts +++ b/src/stores/index.ts @@ -2,9 +2,7 @@ import { reactive, ref } from 'vue' import { defineStore } from 'pinia' import { get, post, put, remove } from "@/tools/request" import dayjs from 'dayjs'; -import { useRouter } from "vue-router" import type { blogInterface, classticInterface, classticFormInterface } from '@/api/admin'; -const router = useRouter() // 博客列表 const bloglist = ref([]) // 语录列表 @@ -83,12 +81,24 @@ export const classticFormStore = defineStore("classform", () => { }) const onSubmit = async (id?: any) => { if (id) { - await put(`/classtics/update/${id}`,classticform) - }else{ - await post("/classtics/add",classticform) + const response = await put(`/classtics/update/${id}`, classticform) + const updatedClasstic = response.data.data; + const index = classticlist.value.findIndex(item => item.id === updatedClasstic.id) + if (index !== -1) { + updatedClasstic.key = classticlist.value[index].key; + classticlist.value[index] = updatedClasstic; + } + } else { + const response=post("/classtics/add", classticform) + const newClasstic = { + ...(await response).data.data, + key: (classticlist.value.length + 1).toString() + } + classticlist.value.push(newClasstic) + } } - return {classticform,onSubmit} + return { classticform, onSubmit } }) // 语录删除 diff --git a/src/views/admin/classticmanage/ClassticManageView.vue b/src/views/admin/classticmanage/ClassticManageView.vue index 377adb7..9a61845 100644 --- a/src/views/admin/classticmanage/ClassticManageView.vue +++ b/src/views/admin/classticmanage/ClassticManageView.vue @@ -61,19 +61,21 @@ const formList = reactive({ mode: "" }) -const id = ref(); +const idRef = ref(); -const openModal = (mode: string, id?: any) => { +const openModal = (mode: string, itemid?: any) => { formList.mode = mode formOpen.value = true; - id.value = id; + idRef.value = itemid; }; const formModal = () => { - if (id.value) { - onSubmit(id.value) + if (idRef.value) { + onSubmit(idRef.value) + formOpen.value = false; } else { onSubmit() + formOpen.value = false; } };