Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
refactor: add mount component method
Browse files Browse the repository at this point in the history
  • Loading branch information
haoziqaq committed Feb 18, 2022
1 parent 85ed28b commit f01a53f
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/varlet-vue2-ui/src/utils/components.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Vue, { ComponentOptions } from 'vue'
import { CombinedVueInstance } from 'vue/types/vue'

export function pickProps<T, U extends keyof T>(props: T, propsKey: U): T[U]
export function pickProps<T, U extends keyof T>(props: T, propsKey: U[]): Pick<T, U>
export function pickProps(props: any, propsKey: any): any {
Expand All @@ -8,3 +11,23 @@ export function pickProps(props: any, propsKey: any): any {
}, {})
: props[propsKey]
}

export interface MountComponentApi {
instance: CombinedVueInstance<any, any, any, any, any>;
unmount(): void;
}

export function mountComponent(component: ComponentOptions<Vue>, container: string, options = {}): MountComponentApi {
const instance = new (Vue.extend(component))(options)
const el = instance.$mount().$el
const wrapper = document.querySelector(container)!
wrapper.appendChild(el)

return {
instance,
unmount() {
instance.$destroy()
wrapper.removeChild(el)
},
}
}

0 comments on commit f01a53f

Please sign in to comment.