UNPKG

wot-design-uni

Version:

一个基于Vue3+TS开发的uni-app组件库,提供70+高质量组件,支持暗黑模式、国际化和自定义主题。

42 lines (34 loc) 842 B
import { ref, inject, computed, onUnmounted, type InjectionKey, getCurrentInstance, type ComponentPublicInstance, type ComponentInternalInstance } from 'vue' type ParentProvide<T> = T & { link(child: ComponentInternalInstance): void unlink(child: ComponentInternalInstance): void children: ComponentPublicInstance[] internalChildren: ComponentInternalInstance[] } export function useParent<T>(key: InjectionKey<ParentProvide<T>>) { const parent = inject(key, null) if (parent) { const instance = getCurrentInstance()! const { link, unlink, internalChildren } = parent link(instance) onUnmounted(() => unlink(instance)) const index = computed(() => internalChildren.indexOf(instance)) return { parent, index } } return { parent: null, index: ref(-1) } }