@nutui/nutui
Version:
京东风格的轻量级移动端 Vue2、Vue3 组件库(支持小程序开发)
22 lines (21 loc) • 522 B
JavaScript
import { inject, getCurrentInstance, onUnmounted, computed } from "vue";
const useParent = (key) => {
const parent = inject(key, null);
if (parent) {
const instance = getCurrentInstance();
const { link, unlink, internalChildren } = parent;
link(instance);
onUnmounted(() => {
unlink(instance);
});
const index = computed(() => internalChildren.indexOf(instance));
return { parent, index };
}
return {
parent,
index: computed(() => -1)
};
};
export {
useParent as u
};