mirror of
https://github.com/youzan/vant.git
synced 2025-10-18 09:24:25 +00:00
chore: rename api folder to composition
This commit is contained in:
27
src/composition/use-relation.ts
Normal file
27
src/composition/use-relation.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
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 {};
|
||||
}
|
Reference in New Issue
Block a user