vite脚手架,配置动态生成路由,添加不同的layout以及meta配置

实现效果,配置了layout和对应的路由的meta

请添加图片描述

  • 我想每个模块添加对应的layout,下边演示一层layout及对应的路由

约束规则:
每个模块下,添加对应的 layout.vue 文件
每个文件夹下的 index.vue 是要渲染的页面路由
每个渲染的页面路由对应的 page.json 是要配置的路由的meta

以下demo目录结构,页面放到了pages下,如果你是在其它文件夹名字,请自己修改
请添加图片描述
以上路径会生成如下结构

[
    {
   
        "path": "/admin",
        "meta": {
   
            "title": "管理系统",
            "isAuth": true
        },
        "children": [
            {
   
                "path": "/admin/about",
                "meta": {
   
                    "title": "关于我们",
                    "isAuth": true
                }
            },
            {
   
                "path": "/admin/home",
                "meta": {
   }
            }
        ],
        "redirect": "/admin/home"
    },
    {
   
        "path": "/crm",
        "meta": {
   },
        "children": [
            {
   
                "path": "/crm/dust",
                "meta": {
   }
            }
        ],
        "redirect": "/crm/dust"
    }
]

直接上代码了,多级有需要的自己递归下

  • router/index.ts
import {
    createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router';

// page module
const pagesModule = import.meta.glob('@/pages/**/index.vue');

// layout module
const layoutModule = import.meta.glob('@/pages/**/layout.vue');

// 获取路由页面的配置
const pagesConfig = import.meta.glob('@/pages/**/page.json', {
   
  eager: true,
  import: 'default'
});

// 处理路径
function getPath(path: string, tag: string) {
   
  return path.replace('/src/pages', '').replace(`/${
     tag}.vue`, '');
}

// 获取页面配置meta
function getMeta(path: string, tag: string): any {
   
  return pagesConfig[path.replace(`${
     tag}.vue`, 'page.json')] || {
   };
}

// 生成layout下路由
function genChildrenRouter(layoutKey: string, obj: any) {
   
  const LAYOUT_KEY = layoutKey.replace('/layout.vue', '');
  for (const pageKey in pagesModule) {
   
    if (pageKey.includes(LAYOUT_KEY)) {
   
      obj.redirect = getPath(pageKey, 'index');
      obj.children.push({
   
        path: getPath(pageKey, 'index'),
        component: pagesModule[pageKey],
        meta: getMeta(pageKey, 'index')
      });
    }
  }
}

// 生成layout
function genLayout() {
   
  const layoutList = [];
  for (const layoutKey in layoutModule) {
   
    const obj: RouteRecordRaw = {
   
      path: getPath(layoutKey, 'layout'),
      component: layoutModule[layoutKey],
      meta: getMeta(layoutKey, 'layout'),
      children: []
    };
    genChildrenRouter(layoutKey, obj);
    layoutList.push(obj);
  }
  return layoutList;
}

// 最终的结果
const routerList = genLayout();

const router = createRouter({
   
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
   
      path: '/',
      redirect: routerList[1].path
    },
    ...routerList
  ]
});

export default router;

相关推荐

  1. mac上添加配置

    2023-12-17 02:14:05       38 阅读
  2. 路由器概述以及静态配置

    2023-12-17 02:14:05       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-17 02:14:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-17 02:14:05       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-17 02:14:05       82 阅读
  4. Python语言-面向对象

    2023-12-17 02:14:05       91 阅读

热门阅读

  1. springBoot使用threadPoolTaskExecutor多线程

    2023-12-17 02:14:05       57 阅读
  2. spring 笔记二 spring配置数据源和整合测试功能

    2023-12-17 02:14:05       46 阅读
  3. Springboot Minio最新版大文件下载

    2023-12-17 02:14:05       65 阅读
  4. C 标准库 - <string.h>

    2023-12-17 02:14:05       47 阅读
  5. echarts 柱形图、折线图点击事件

    2023-12-17 02:14:05       53 阅读
  6. Docker笔记:简单部署 nodejs 项目和 golang 项目

    2023-12-17 02:14:05       55 阅读
  7. Python中的名称空间和作用域

    2023-12-17 02:14:05       60 阅读
  8. NLP中的Seq2Seq与attention注意力机制

    2023-12-17 02:14:05       56 阅读
  9. Unity项目里Log系统该怎么设计

    2023-12-17 02:14:05       49 阅读
  10. docker配置容器内访问主机服务

    2023-12-17 02:14:05       64 阅读
  11. axios+echarts

    2023-12-17 02:14:05       50 阅读
  12. react受控组件和非受控组件区别

    2023-12-17 02:14:05       53 阅读
  13. 大语言模型续写completions

    2023-12-17 02:14:05       58 阅读