Vue-Router(4) 学习之动态路由二

1. 动态路由表 dynamic_routes


export const dynamic_routes = [
  {
    path: '/origin_setting',
    component: Layout,
    redirect: '/origin_setting/index',
    depthFlagArr: [0,1],
    name: 'MyOriginSetting',
    children: [
      {
        path: '/origin_setting/index',
        name: 'OriginSetting',
        meta: { title: '域名管理', icon: Setting },
        component: () => import('../views/OriginSetting/index.vue')
      }
    ]
  },
  {
    path: '/avatar-manage',
    component: Layout,
    redirect: '/avatar-manage/index',
    // depthFlagArr: [],
    show_ivl_menu: true,
    name: 'MyAvatarManage',
    children: [
      {
        path: '/avatar-manage/index',
        name: 'AvatarManage',
        meta: { title: '形象管理', icon: Avatar },
        component: () => import('../views/AvatarManage/index.vue')
      }
    ]
  }
];

说明:depthFlagArr,show_ivl_menu 是判断是否加载路由的标识

2. 路由控制 private_routes.ts

使用pinia作为全局store,控制包括路由权限、userinfo、token等动态信息
 

// private_routes.ts 路由控制
import { defineStore } from 'pinia'
import { computed } from 'vue';
import router from '@/router'

import { useAuthStore } from '@/store/user'
import { dynamic_routes } from '@/router/menuRoutes'
// 动态路由菜单
export const useDynamicMenuStore = defineStore('dynamicRouter', () => {
  const userStore = useAuthStore()
  const _id = computed(() => userStore.info._id)
  const depth = computed(() => userStore.info.depth)
  const show_ivl_menu = computed(() => userStore.info.show_ivl_menu)
  const menuRoutes = computed(() => {
    console.log(_id.value)
    if (!_id.value) return []
    // 使用computed根据role筛选对应路由
    return dynamic_routes.filter(route => {
      if (route.depthFlagArr && route.depthFlagArr.includes(depth.value))  {
        router.addRoute(route) // Add the route to 
        return true;
      } else if(route.show_ivl_menu && show_ivl_menu.value) {
        router.addRoute(route) // Add the route to 
        return true;
      }
    })
  })

  return { _id, menuRoutes }
});

3. 菜单栏

显示在左侧的导航栏

<template>
    <el-aside class="page-side">
      <el-scrollbar>
        <el-menu
          :router="true"
          active-text-color="#ffd04b"
          background-color="#545c64"
          class="el-menu-vertical-demo"
          :default-active="activePath"
          text-color="#fff"
        >
          <template v-for="item in routes" :key="item.path">
            <template v-if="item.children.length === 1">
              <el-menu-item :index="item.children[0].path">
                <el-icon>
                  <component :is="item.children[0].meta.icon" />
                </el-icon>
                <span>{{ item.children[0].meta.title }}</span>
              </el-menu-item>
            </template>

            <el-sub-menu v-if="item.children.length > 1" :index="item.path">
              <template #title>
                <el-icon><icon-menu /> </el-icon>
                {{ item.meta?.title }}
              </template>
              <el-menu-item
                v-for="child in item.children"
                :key="child.path"
                :index="child.path"
                >
                <el-icon><icon-menu /> </el-icon>
                {{ child.meta.title }}
              </el-menu-item>
            </el-sub-menu>
          </template>
        </el-menu>
      </el-scrollbar>
    </el-aside>
</template>

<script setup lang="ts">
import { computed } from "vue";
// 从路由中获取菜单
import { private_routes } from "@/router/menuRoutes";
import { useDynamicMenuStore } from '@/store/private_routes';

import { Menu as IconMenu } from "@element-plus/icons-vue";
// 获取当前 路由的 path
import { useRoute } from "vue-router";
const route = useRoute();
// 获取当前 路由的 path
const activePath = computed(() => route.path);

const dynamicMeneStore = useDynamicMenuStore()
const menu = computed(() =>dynamicMeneStore.menuRoutes)

// 判断动态路由是否需要移除
const routes = computed(() => {
  return private_routes.concat(menu.value)
})
</script>

<style scoped>
.page-side {
  background: #545c64;
  display: flex;
  flex-wrap: wrap;
  width: 150px;
}

.el-menu {
  border-right: none;
}
.el-scrollbar {
  height: 100%;
  width: 100%;
}
</style>

4. 问题思考

        4.1 动态路由刷新404

        4.2 动态路由与userInfo的关系

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值