From dd95462545445739e8f611938799bb138a97e7d8 Mon Sep 17 00:00:00 2001 From: maoyiluo Date: Fri, 28 Feb 2025 16:27:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DmessagePlugin=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=AE=9E=E4=BE=8B=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/message/messageList.tsx | 10 ++++++++-- src/message/plugin.tsx | 8 +++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/message/messageList.tsx b/src/message/messageList.tsx index 2bb789c05..cfd26b38e 100644 --- a/src/message/messageList.tsx +++ b/src/message/messageList.tsx @@ -40,13 +40,14 @@ export const MessageList = mixins(classPrefixMixins).extend({ }, methods: { add(msg: MessageOptions): number { + const key = getUniqueId(); const mg = { ...msg, - key: getUniqueId(), + key, placement: this.placement, }; this.list.push(mg); - return this.list.length - 1; + return key; }, remove(index: number) { this.list.splice(index, 1); @@ -74,6 +75,11 @@ export const MessageList = mixins(classPrefixMixins).extend({ 'duration-end': () => this.remove(index), }; }, + getMessageInstance(key: string) { + const index = this.list.findIndex((msgOption) => msgOption.key === key); + if (index === -1) return null; + return this.$children[index]; + }, }, render(): VNode { if (!this.list.length) return; diff --git a/src/message/plugin.tsx b/src/message/plugin.tsx index 85e96c8e3..03986144b 100644 --- a/src/message/plugin.tsx +++ b/src/message/plugin.tsx @@ -71,6 +71,8 @@ const MessageFunction = (props: MessageOptions): Promise => { } const p = instanceMap.get(attachDom)[placement]; + let messageKey: number | null = null; + // attachDom被清空(如attachDom.innerHtml=''),p也会存在,需要判断下dom包含关系 if (!p || !attachDom.contains(p.$el)) { const instance = new MessageList({ @@ -79,18 +81,18 @@ const MessageFunction = (props: MessageOptions): Promise => { placement: options.placement, }, }).$mount(); - instance.add(options); + messageKey = instance.add(options); instanceMap.get(attachDom)[placement] = instance; attachDom.appendChild(instance.$el); } else { - p.add(options); + messageKey = p.add(options); } // 返回最新消息的 Element return new Promise((resolve) => { const ins = instanceMap.get(attachDom)[placement]; ins.$nextTick(() => { const msg: Array = ins.$children; - resolve(msg[msg.length - 1]); + resolve(messageKey ? ins.getMessageInstance(messageKey) : msg[msg.length - 1]); }); }); };