chore(Tabbar): use relation

This commit is contained in:
chenjiahan
2020-09-26 11:13:49 +08:00
parent 11890ef3c1
commit 367a273bd5
3 changed files with 9 additions and 38 deletions
-30
View File
@@ -1,30 +0,0 @@
import { inject, computed, onUnmounted } from 'vue';
type Parent = { children: unknown[] };
type Child<T> = T extends { children: (infer U)[] } ? U : never;
export function useParent<P extends Parent>(
key: string,
child = {} as Child<P>
) {
const parent = inject<P | null>(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 {};
}