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