vue3添加动态路由

1.pinia中的处理后端返回的路由信息格式和存储

async getMenuInfo(router) {
            const useMenusStore = menusStore();
            //向Menus push数据
         
            let res = await getMenus();
            if (res.status === 'ok') {
                useMenusStore.pushMenus({
                    resPath: '',
                    resName: '',
                    icon: '',
                    id: '',
                    children: res.data,
                });
                let isLogin = getCookie('nmp_username');
                
                // 动态添加外链路由配置
                let routerList = formatRoute(res.data);
                routerList.forEach((item) => {
                    //router是从 router/index传入的router对象
                    router.addRoute(item);
                });
            }

            return true;
        },
//根据自己项目需求  将后端返回的路由数据配置为 路由需要的格式
const formatRoute = (resRouterList) => {
    let oneList = getOneArr(JSON.parse(JSON.stringify(resRouterList)));
    let isFrameList = oneList.filter((item) => item.is_frame === 1);

    let arr = [];
    isFrameList.forEach((item) => {
        if (item.parent === 'index') {
            item.path = '/' + item.parent + '/' + item.id;
            arr.push({
                path: item.path,
                component: Home,
                redirect: item.path,
                meta: {
                    title: item.name,
                    tagShow: false,
                },
                children: [
                    {
                        path: item.path,
                        name: item.parent + item.id,
                        meta: {
                            title: item.name,
                        },
                        component: () => import('@/views/inline/inline.vue'),
                    },
                ],
            });
        } else {
            let outPath = getOnePath(item, oneList);

            item.path = outPath + '/' + item.id;

            arr.push({
                path: outPath,
                component: Home,
                redirect: item.path,
                meta: {
                    title: item.name,
                    tagShow: false,
                },
                children: [
                    {
                        path: item.path,
                        name: item.parent + item.id,
                        meta: {
                            title: item.name,
                        },
                        component: () => import('@/views/inline/inline.vue'),
                    },
                ],
            });
        }
    });

    return arr;
};

2.router路由表中导航守卫

防止刷新页面丢失路由菜单,我这里用pinia里的hasMenus 来判断。但是设置hasMenus为true时,不要在pinia仓库修改。之后next要写为next({ ...to, replace: true }); 否则空白页

router.beforeEach(async (to, from, next) => {
    // 获取菜单
    // 判断登录后 再获取 登陆后的菜单
    let isLogin = getCookie('nmp_username');
    const userInfoStore = userStore();
    // if (isLogin) {
    // } else {
    // }
    if (to.path === '/login' || to.path === '/menhu' || to.path === '/') {
        !userInfoStore.hasMenus ? await userInfoStore.getMenuInfo(router) : '';
        next();
    } else {
        if (isLogin) {
            //存用户信息
            await userInfoStore.setUserInfo();
            if (!userInfoStore.hasMenus) {
                await userInfoStore.getMenuInfo(router);
                userInfoStore.hasMenus = true;
                next({ ...to, replace: true });
            } else {
                next();
            }
        } else {
            !userInfoStore.hasMenus
                ? await userInfoStore.getMenuInfo(router)
                : '';
            next('/login');
        }
    }
});

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值