123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import type { SubMenuProvider } from '../interface';
- import { computed, getCurrentInstance } from 'vue';
- import { findComponentUpward } from '../utils';
- function useMenu() {
- const instance = getCurrentInstance();
- if (!instance) {
- throw new Error('instance is required');
- }
- /**
- * @zh_CN 获取所有父级菜单链路
- */
- const parentPaths = computed(() => {
- let parent = instance.parent;
- const paths: string[] = [instance.props.path as string];
- while (parent?.type.name !== 'Menu') {
- if (parent?.props.path) {
- paths.unshift(parent.props.path as string);
- }
- parent = parent?.parent ?? null;
- }
- return paths;
- });
- const parentMenu = computed(() => {
- return findComponentUpward(instance, ['Menu', 'SubMenu']);
- });
- return {
- parentMenu,
- parentPaths,
- };
- }
- function useMenuStyle(menu?: SubMenuProvider) {
- const subMenuStyle = computed(() => {
- return {
- '--menu-level': menu ? (menu?.level ?? 0 + 1) : 0,
- };
- });
- return subMenuStyle;
- }
- export { useMenu, useMenuStyle };
|