Files
vant/src/api/use-relation.ts
2020-08-26 11:34:16 +08:00

28 lines
515 B
TypeScript

import { inject, computed, onUnmounted } from 'vue';
export type Parent<T = unknown> = null | {
children: T[];
};
export function useParent<T = unknown>(key: string, child: T = {} as T) {
const parent = inject<Parent<T>>(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 {};
}