-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
mount.ts
49 lines (40 loc) · 1.04 KB
/
mount.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import type { InjectionKey, Ref } from 'vue-demi'
import { createApp, defineComponent, h, provide, ref } from 'vue-demi'
type InstanceType<V> = V extends { new (...arg: any[]): infer X } ? X : never
type VM<V> = InstanceType<V> & { unmount: () => void }
export function mount<V>(Comp: V) {
const el = document.createElement('div')
const app = createApp(Comp as any)
const unmount = () => app.unmount()
const comp = app.mount(el) as any as VM<V>
comp.unmount = unmount
return comp
}
export function useSetup<V>(setup: () => V) {
const Comp = defineComponent({
setup,
render() {
return h('div', [])
},
})
return mount(Comp)
}
export const Key: InjectionKey<Ref<number>> = Symbol('num')
export function useInjectedSetup<V>(setup: () => V) {
const Comp = defineComponent({
setup,
render() {
return h('div', [])
},
})
const Provider = defineComponent({
components: Comp,
setup() {
provide(Key, ref(1))
},
render() {
return h('div', [])
},
})
return mount(Provider)
}