feat: add useParent/useChildren

This commit is contained in:
chenjiahan 2020-08-23 15:06:56 +08:00
parent 9644047dc7
commit 1f82bcf5f0
5 changed files with 31 additions and 2 deletions

View File

@ -5,7 +5,7 @@ import {
onMounted, onMounted,
onActivated, onActivated,
onUnmounted, onUnmounted,
onDeactivated onDeactivated,
} from 'vue'; } from 'vue';
export function useGlobalEvent( export function useGlobalEvent(

29
src/api/use-relation.ts Normal file
View File

@ -0,0 +1,29 @@
import { ref, Ref, provide, inject, computed } from 'vue';
export type Parent = null | {
children: Ref<unknown[]>;
};
export function useParent(key: string) {
const children = ref<unknown>([]);
provide(key, { children });
return {
children,
};
}
export function useChildren(key: string, child: unknown) {
const parent = inject<Parent>(key, null);
const index = computed(() => parent?.children.value.indexOf(child));
if (parent) {
parent.children.value.push(child);
}
return {
index,
parent,
};
}

View File

@ -58,6 +58,6 @@ export function useTouch() {
deltaY, deltaY,
offsetX, offsetX,
offsetY, offsetY,
direction direction,
}; };
} }