From fb0f3c1a871c7fb9d60247882687ed57c868a7a0 Mon Sep 17 00:00:00 2001 From: good-ideal <83727128@qq.com> Date: Tue, 28 Jun 2022 15:05:09 +0800 Subject: [PATCH] =?UTF-8?q?refactor(.):=20=E2=99=BB=EF=B8=8F=20toast?= =?UTF-8?q?=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit toast部分代码重构, 提交可维护性. --- packages/components/toast/index.ts | 13 ++----- packages/components/toast/index.vue | 33 ++++-------------- packages/components/toast/toastTypes.ts | 46 +++++++++++++++++++++++++ packages/utils/dom.ts | 29 ++++++++++++++++ packages/utils/index.ts | 1 + 5 files changed, 86 insertions(+), 36 deletions(-) create mode 100644 packages/components/toast/toastTypes.ts create mode 100644 packages/utils/dom.ts diff --git a/packages/components/toast/index.ts b/packages/components/toast/index.ts index 48a3f65..8e8d540 100644 --- a/packages/components/toast/index.ts +++ b/packages/components/toast/index.ts @@ -1,5 +1,5 @@ -import { h, render } from 'vue' import toast from './index.vue' +import { createGlobalNode, removeGlobalNode } from '~/utils' import type { ToastApi } from './index.vue' // 函数式调用 @@ -7,17 +7,10 @@ export function UToast(options: ToastApi) { let duration = options.duration if (!options.message) return options.duration = duration ? duration : 1000 - // 根据components定义生成虚拟DOM - const vnode = h(toast, options) - // 需要创建一个容器div来渲染这个虚拟节点 - const div = document.createElement('div') - // 然后将container append 到body上面 - document.body.append(div) - // 这样就成功将虚拟节点渲染到 div - render(vnode, div) + const { vnode, div } = createGlobalNode(toast, options) setTimeout(() => { - render(null, div) + removeGlobalNode(div) }, options.duration + 300) return vnode } diff --git a/packages/components/toast/index.vue b/packages/components/toast/index.vue index f619fd9..d9fd617 100644 --- a/packages/components/toast/index.vue +++ b/packages/components/toast/index.vue @@ -13,6 +13,7 @@