您的位置:首页 > 财经 > 产业 > 装修平台哪家好_世界环境日_百度投诉中心电话_百度推广官方网站登录入口

装修平台哪家好_世界环境日_百度投诉中心电话_百度推广官方网站登录入口

2024/10/5 20:22:17 来源:https://blog.csdn.net/qq_58706693/article/details/142623178  浏览:    关键词:装修平台哪家好_世界环境日_百度投诉中心电话_百度推广官方网站登录入口
装修平台哪家好_世界环境日_百度投诉中心电话_百度推广官方网站登录入口
  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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com