0
点赞
收藏
分享

微信扫一扫

vue3.x:关于侧边菜单栏多页面激活的想法

耳一文 2022-02-11 阅读 99

1.在meta中加入activeMenu,如下

children: [
      {
        path: 'list',
        name: 'billList',
        meta: {
          title: '单据列表',
        },
        component: () => import('@/views/bill/billList/index.vue'),
      },
      {
        path: 'config',
        name: 'billConfig',
        meta: {
          hidden: true,
          title: '单据配置项',
          activeMenu: 'billList',
        },
        component: () => import('@/views/bill/billConfig/index.vue'),
      }}

看到第二个billConfig中的meta中加入了activeMenu,意思为当进入billConfig页面中激活的是billList这个侧边栏的。

当然,这样还是不够的,我们还需要在侧边菜单栏中进行处理,如下

  import { useRoute, useRouter } from 'vue-router';    
   import { ref,  watch} from 'vue';
  const currentRoute = useRoute();

  //选择的菜单栏 ,表现形式如['index1']
  const selectedKeys = ref<string[]>([currentRoute.name as string]); 

 // 在watch中对激活进行判断
  watch(
    () => currentRoute.fullPath,
    () => {
    	// 获取到meta中有无配置激活的字段 activeMenu	
      const activeMenu: string = (currentRoute.meta?.activeMenu as string) || '';
      selectedKeys.value = activeMenu ? [activeMenu] : ([currentRoute.name] as string[]);
    },
  );
举报

相关推荐

0 条评论