当前位置: 首页 > article >正文

Vue3动态导入后端路由

  1. 动态获取文件
// modules就是一个map,文件路径作为key,文件对象作为value
const modules=import.meta.glob('../views/**/*.vue')

在这里插入图片描述

获取到的modules对象如下:

在这里插入图片描述

  1. 添加动态路由
import {createRouter, createWebHashHistory} from 'vue-router'

const modules = import.meta.glob('../views/**/*.vue')

const constantRoutes = [
    {
        path: '/login',
        name: 'login',
        meta: {
            title: '登录',
            hidden: true
        },
        component: () => import('../views/login/index.vue')
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes:[…constantRoutes]
})

// 从后端动态获取的路由列表,[hidden:是否在侧边栏显示]
let dynamicRoutes = [
    {
        path: '/',
        name: 'Layout',
        redirect: '/home',
        component: '/views/layout/index',
        meta: {
            title: 'Layout',
            icon: 'HomeFilled',
            hidden: false
        },
        children: [
            {
                path: '/home',
                name: 'Home',
                component: '/views/home/index',
                meta: {
                    title: '首页',
                    icon: 'HomeFilled',
                    hidden: false
                }
            },
        ]
    },
    {
        path: '/sys',
        name: 'Sys',
        component: '/views/layout/index',
        meta: {
            title: '系统管理',
            icon: 'document',
            hidden: false
        },
        children: [
            {
                path: '/sys/user',
                name: 'User',
                component: '/views/sys/user/index',
                meta: {
                    title: '用户管理',
                    icon: 'document',
                    hidden: false
                }
            },
            {
                path: '/sys/role',
                name: 'Role',
                component: '/views/sys/role/index',
                meta: {
                    title: '角色管理',
                    icon: 'document',
                    hidden: false
                }
            },
        ]
    }
]

convertPathToComponent(dynamicRoutes)
addDynamicRoutes(dynamicRoutes)

// 添加动态路由
function addDynamicRoutes(dynamicRoutes) {
    dynamicRoutes.forEach(item => {
        router.addRoute(item)
    })
}

// 使用递归将routeList中每个对象及其子对象中的component路径转换为组件(在component不为空的情况下)
function convertPathToComponent(dynamicRoutes) {
    dynamicRoutes.forEach(item => {
        if (item.component) {
            item.component = modules['..' + item.component + '.vue'] || modules['../views/404/index.vue']
        }
        if (item.children) {
            convertPathToComponent(item.children)
        }
    })
}

export default router


http://www.kler.cn/news/331224.html

相关文章:

  • 使用 Vue3 和 Axios 实现 CRUD 操作
  • Linux忘记root用户密码怎么重设密码
  • SpringCloud Config配置中心 SpringCloud Bus消息总线
  • SQL基础教程
  • linux系统解压zip文件名乱码
  • vue3项目执行pnpm update后还原package.json文件后运行报错
  • 7.使用 VSCode 过程中的英语积累 - Terminal 菜单(每一次重点积累 5 个单词)
  • docker快速安装ELK
  • IDEA在git提交时添加忽略文件
  • 【动态规划-分组背包】【hard】力扣2218. 从栈中取出 K 个硬币的最大面值和
  • C++ 类和对象的初步介绍
  • 网页前端开发之Javascript入门篇(3/9):条件控制
  • Vue.js 组件开发知识详解
  • 国外电商系统开发-运维系统开发
  • python如何查询函数
  • pod管理及优化
  • 解决 MySQL 服务无法启动:failed to restart mysql.service: unit not found
  • 2024-09-04 深入JavaScript高级语法十五——浏览器原理-V8引擎-js执行原理
  • Springboot项目jar包中resource目录下静态资源excel、pdf等二进制文件损坏
  • 跨平台音乐播放器Feishin