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[]);
},
);