vue3动态路由的实现以及目录权限的设置

在 Vue 3、TypeScript 和 Vite 项目中,设置动态目录以及目录权限,通常涉及到路径别名的配置、动态路由的实现以及权限控制的逻辑。以下是具体的实现步骤:

1. 动态路由的实现

定义静态路由

router/index.ts 中定义静态路由,例如登录页、首页等。

TypeScript 复制代码
import { createRouter, createWebHistory } from 'vue-router';
import Layout from '@/layout/index.vue';

export const constantRoutes = [
  {
    path: '/',
    redirect: '/dashboard',
    hidden: true,
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    meta: {
      title: '首页',
      icon: 'House',
    },
    component: Layout,
  },
  {
    path: '/login',
    name: 'Login',
    meta: {
      title: '登录',
    },
    component: () => import('@/views/user/login/index.vue'),
    hidden: true,
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/error/404.vue'),
    hidden: true,
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
});

export default router;
动态加载路由

创建一个函数来动态加载路由,通常在权限相关的 store 中实现。

TypeScript 复制代码
import { ref } from 'vue';
import router from '@/router';
import type { RouteRecordRaw } from 'vue-router';

const asyncRoutes = ref<RouteRecordRaw[]>([]);

export const useRouterConfig = () => {
  const modules = import.meta.glob('@/views/**');
  const addRoutes = (menus: RouteRecordRaw[]) => {
    asyncRoutes.value = menus;
    filterAsyncRouter();
    router.addRoute({
      path: '/',
      redirect: asyncRoutes.value[0].path,
    });
  };

  const filterAsyncRouter = () => {
    const routerLoop = (routes: RouteRecordRaw[], ParentName?: string) => {
      routes.forEach((item) => {
        if (item.component === 'Layout') {
          item.component = () => import('@/layout/index.vue');
        } else {
          item.component = resolveComponent(item.component);
        }
        const { title, show, icon, name, path, component, children } = item;
        const route: RouteRecordRaw = {
          component,
          path,
          name,
          meta: {
            title,
            show,
            icon,
          },
          children: children as any,
        };

        if (ParentName) {
          router.addRoute(ParentName, route);
        } else {
          router.addRoute(route);
        }

        if (item.children && item.children.length > 0) {
          routerLoop(item.children, item.name);
        }
      });
    };

    routerLoop(asyncRoutes.value);
  };

  const resolveComponent = (path: string) => {
    const importPage = modules[`../views${path}`];
    if (!importPage) {
      throw new Error(`Unknown page ${path}. Is it located under Pages with a .vue extension?`);
    }
    return importPage;
  };

  return { addRoutes };
};

2. 权限控制

在导航守卫中进行权限控制,确保用户只能访问其有权限的路由。

TypeScript 复制代码
import router from './index';
import { useUserStore } from '@/store/user';

const whiteList = ['/login', '/404'];

router.beforeEach(async (to) => {
  const token = localStorage.getItem('token');
  if (whiteList.includes(to.path) && !token) {
    return true;
  } else {
    const { menuList, getMenuList } = useUserStore();
    if (!menuList.length) {
      await getMenuList();
      return { path: to.fullPath };
    }
  }
});

3. 获取后端路由数据

store/user.ts 中定义获取用户菜单列表的逻辑。

TypeScript 复制代码
import { defineStore } from 'pinia';
import { ref } from 'vue';

export const useUserStore = defineStore('user', () => {
  const menuList = ref([]);
  const getMenuList = async () => {
    // 这里是获取后端路由数据的 API 调用
    // 模拟获取数据
    menuList.value = [
      {
        path: '/dashboard',
        name: 'Dashboard',
        meta: { title: '首页', icon: 'House' },
        component: 'Layout',
      },
      {
        path: '/user',
        name: 'User',
        meta: { title: '用户管理', icon: 'User' },
        component: 'Layout',
        children: [
          {
            path: '/user/list',
            name: 'UserList',
            meta: { title: '用户列表' },
            component: () => import('@/views/user/list.vue'),
          },
        ],
      },
    ];
  };

  return { menuList, getMenuList };
});

4. 动态路由的初始化

main.ts 中初始化动态路由。

TypeScript 复制代码
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import { useRouterConfig } from './router/config';
import { useUserStore } from './store/user';

const app = createApp(App);
app.use(router);

// 初始化动态路由
const { addRoutes } = useRouterConfig();
const userStore = useUserStore();
addRoutes(userStore.menuList);

app.mount('#app');

通过以上步骤可以实现 Vue 3、TypeScript 和 Vite 项目中的动态目录设置和目录权限控制。这包括了路径别名的配置、动态路由的实现以及权限控制的逻辑。

相关推荐
lunz_fly1992几秒前
统信 UOS 安装 svn 指南
linux
Antonio91513 分钟前
【Redis】Linux 配置Redis
linux·数据库·redis
IT成长日记20 分钟前
【自动化运维神器Ansible】Ansible常用模块之archive模块详解
运维·自动化·ansible·常用模块·archive
晴天¥25 分钟前
阶段1--域名服务器
运维·服务器·网络
Rover.x41 分钟前
内存泄漏问题排查
java·linux·服务器·缓存
禁默42 分钟前
进程调度的艺术:从概念本质到 Linux 内核实现
linux·运维·服务器
CLO_se_1 小时前
嵌入式软件面试八股文
linux·面试
Azure DevOps1 小时前
在Azure DevOps的工作项中使用markdown
运维·microsoft·flask·azure·devops
先做个垃圾出来………1 小时前
CI/CD与DevOps集成方法
运维·ci/cd·devops
qq_447705311 小时前
宝塔通过docker部署JupyterHub指南【常见错误处理】
运维·docker·容器