mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-23 09:52:57 +08:00
feat: add useParent/useChildren
This commit is contained in:
parent
9644047dc7
commit
1f82bcf5f0
@ -5,7 +5,7 @@ import {
|
||||
onMounted,
|
||||
onActivated,
|
||||
onUnmounted,
|
||||
onDeactivated
|
||||
onDeactivated,
|
||||
} from 'vue';
|
||||
|
||||
export function useGlobalEvent(
|
29
src/api/use-relation.ts
Normal file
29
src/api/use-relation.ts
Normal 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,
|
||||
};
|
||||
}
|
@ -58,6 +58,6 @@ export function useTouch() {
|
||||
deltaY,
|
||||
offsetX,
|
||||
offsetY,
|
||||
direction
|
||||
direction,
|
||||
};
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user