vue-路由自动化

Router

介绍

工程中的Router会根据src/views目录结构自动生成路由配置,路由结构与文件系统结构保持一致,不需要手动编写routes = [...],使得项目结构更加直观和易于理解。


配置

配置文件:

 router
    └─ index.ts
import {
   createRouter, createWebHistory, RouteRecordRaw} from "vue-router";


const ps = import.meta.glob("../views/**/page.ts", {
   eager: true, import: 'default'})
const cs = import.meta.glob("../views/**/index.vue")
const re: string[] = []
function createChildRoutes(basePath: string): RouteRecordRaw[] {
   
    const pObjs = Object.entries(ps)
    const routes: RouteRecordRaw[] = pObjs.filter(([path]) => {
   
        return path.startsWith(basePath) && path.split('/').length === basePath.split('/').length + 1;
    }).map(([path, params]) => {
   
        return createRoutes(path, params, (path) => {
   
            path = path.replace('../views', '').replace('/page.ts', '').replace('[', ':').replace(']', '')
            const name = path.replace(':', '').split('/').filter(Boolean).join('-') || 'index'
            const pathArr =  path.split('/')
            const routePath = pathArr[pathArr.length - 1]
            re.push(name)
            return [routePath, name
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值