init
This commit is contained in:
32
uni_modules/nutui-uni/components/_hooks/useInject.ts
Normal file
32
uni_modules/nutui-uni/components/_hooks/useInject.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { computed, getCurrentInstance, inject, onUnmounted, ref } from 'vue'
|
||||
import type { ComponentInternalInstance, InjectionKey } from 'vue'
|
||||
|
||||
type ParentProvide<T> = T & {
|
||||
add: (child: ComponentInternalInstance) => void
|
||||
remove: (child: ComponentInternalInstance) => void
|
||||
internalChildren: ComponentInternalInstance[]
|
||||
}
|
||||
|
||||
export function useInject<T>(key: InjectionKey<ParentProvide<T>>) {
|
||||
const parent = inject(key, null)
|
||||
|
||||
if (parent) {
|
||||
const instance = getCurrentInstance()!
|
||||
const { add, remove, internalChildren } = parent
|
||||
|
||||
add(instance)
|
||||
onUnmounted(() => remove(instance))
|
||||
|
||||
const index = computed(() => internalChildren.indexOf(instance))
|
||||
|
||||
return {
|
||||
parent,
|
||||
index,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
parent: null,
|
||||
index: ref(-1),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user