mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
31 lines
571 B
TypeScript
31 lines
571 B
TypeScript
import { inject, computed, onUnmounted } from 'vue';
|
|
|
|
type Parent = { children: unknown[] };
|
|
|
|
type Child<T> = T extends { children: (infer U)[] } ? U : never;
|
|
|
|
export function useParent<P extends Parent>(
|
|
key: string,
|
|
child = {} as Child<P>
|
|
) {
|
|
const parent = inject<P | null>(key, null);
|
|
|
|
if (parent) {
|
|
const { children } = parent;
|
|
const index = computed(() => children.indexOf(child));
|
|
|
|
children.push(child);
|
|
|
|
onUnmounted(() => {
|
|
children.splice(index.value, 1);
|
|
});
|
|
|
|
return {
|
|
index,
|
|
parent,
|
|
};
|
|
}
|
|
|
|
return {};
|
|
}
|