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]); }); }); };