From 565186e51fa5a5067762bf95b550e9fe47a18fc0 Mon Sep 17 00:00:00 2001 From: iamgx Date: Fri, 14 Jul 2023 17:18:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=A6=86=E7=9B=96?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=95=B0=E6=8D=AE=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 13 +++++++------ package.json | 2 +- src/index.ts | 13 +++++++++++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7eceaef..acff0e0 100644 --- a/README.md +++ b/README.md @@ -27,12 +27,13 @@ this.$toast({ message: 'hello world' }) #### Options -| 参数名 | 说明 | 类型 | 默认值 | -| --------- | -------------------- | -------- | --------------- | -| name | 方法名 | String | $dynamicMount | -| extend | 挂载的组件 (必填) | VNode | - | -| nextTick | 挂载后的回调 | Function | - | -| className | 挂载节点自定义 class | String | dynamic-element | +| 参数名 | 说明 | 类型 | 默认值 | +| ------------- | -------------------- | ------------------- | --------------- | +| name | 方法名 | String | $dynamicMount | +| extend | 挂载的组件 (必填) | VNode | - | +| nextTick | 挂载后的回调 | Function | - | +| className | 挂载节点自定义 class | String | dynamic-element | +| defaultOption | extand 组件默认配置 | Record | {} | #### Example diff --git a/package.json b/package.json index f9d06f9..9dd1bab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "use-dynamic-mount", - "version": "1.0.2", + "version": "1.0.4", "main": "dist/index.esm.js", "module": "dist/index.esm.js", "browser": "dist/index.umd.js", diff --git a/src/index.ts b/src/index.ts index 7693dba..88a6452 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ export interface DynamicMountOptions { name?: string className?: string extend?: any + defaultOption?: Record nextTick?: () => void } @@ -14,13 +15,20 @@ export interface MountReturnValue { export default () => ({ install(Vue: any, options?: DynamicMountOptions) { - const { name = '$dynamicMount', className = 'dynamic-element', extend, nextTick } = options || {} + const { + name = '$dynamicMount', + className = 'dynamic-element', + extend, + nextTick, + defaultOption = {} + } = options || {} if (!extend) return Vue.prototype[name] = (options: any): MountReturnValue => { const DynamicComponent = Vue.extend(extend) const app = new DynamicComponent({ data: () => ({ + ...defaultOption, ...options, destroy: () => destroy() }) @@ -29,6 +37,7 @@ export default () => ({ const destroy = () => { app.$el.remove() app.$destroy() + options.onDestroy?.() } const div = document.createElement('div') @@ -40,7 +49,7 @@ export default () => ({ return { app, destroy, - close: () => app.close && app.close(), + close: () => app.close?.(), component: () => app.$refs.component } }