Skip to content

Commit

Permalink
feat: 增加覆盖默认数据参数
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgx committed Jul 14, 2023
1 parent 2781139 commit 565186e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any> | {} |

#### Example

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface DynamicMountOptions {
name?: string
className?: string
extend?: any
defaultOption?: Record<string, any>
nextTick?: () => void
}

Expand All @@ -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()
})
Expand All @@ -29,6 +37,7 @@ export default () => ({
const destroy = () => {
app.$el.remove()
app.$destroy()
options.onDestroy?.()
}

const div = document.createElement('div')
Expand All @@ -40,7 +49,7 @@ export default () => ({
return {
app,
destroy,
close: () => app.close && app.close(),
close: () => app.close?.(),
component: () => app.$refs.component
}
}
Expand Down

0 comments on commit 565186e

Please sign in to comment.