tools-vue3
TypeScript icon, indicating that this package has built-in type declarations

2.0.12 • Public • Published

vue3 工具

CRouter 复杂示例

import System from '@/utils/System'
import { createRouter, createWebHashHistory } from 'vue-router'
import CRouter from './CRouter'

/**
 * 默认主题
 */
const themeList = ['light', 'dark'] as any[]
const defaultTheme = 'light' as any
const diffTheme = 'dark' as any

/**
 * 路由目录/src/views/light/home/index.vue、/src/views/dark/home/index.vue
 */
export const initRouter = () => {
  let _modules = import.meta.glob([
    '@/views/**/*.vue',
    '!@/views/**/*-com/**/*.vue',
    '!@/views/**/com/**/*.vue',
    '!@/views/**/components/**/*.vue'
  ])

  const _defaultModules = {} as any
  const _diffModules = {} as any
  Object.keys(_modules).forEach((key) => {
    themeList.forEach((item) => {
      if (defaultTheme != item) {
        if (!new RegExp(`^/src/views/${item}`).test(key)) {
          _defaultModules[key] = _modules[key]
        }
        if (diffTheme != defaultTheme && new RegExp(`^/src/views/${diffTheme}`).test(key)) {
          _diffModules[key] = _modules[key]
        }
      }
    })
  })

  const pathHook = (path: string) => {
    return path
      .replace(/\/index$/, '')
      .replace(new RegExp(`/${defaultTheme}/`), '/')
      .replace(new RegExp(`/${diffTheme}/`), '/')
  }

  const _routes = CRouter.init({
    modules: _defaultModules,
    diffModules: _diffModules,

    children: [
      {
        path: '/layout',
        component: '/layout',
        childrenReg: ['^/pages']
      }
    ],
    setInfoHook(item) {
      item.meta = {
        title: item.path
      }
    },
    mapKeyPathHook(path) {
      return pathHook(path)
    },
    pathHook(path) {
      return pathHook(path).replace(/^\/pages/, '')
    }
  })

  const router = createRouter({
    history: createWebHashHistory(),
    routes: [
      ..._routes.array,
      {
        path: '/',
        redirect: '/home'
      }
    ]
  })

  System.router = router

  return router
}

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.0.126latest

Version History

VersionDownloads (Last 7 Days)Published
2.0.126
2.0.114
2.0.103
2.0.90
2.0.80
2.0.72
2.0.60
2.0.53
2.0.41
2.0.32
2.0.20
2.0.10
2.0.00
1.2.11
1.2.00
1.1.74
1.1.60
1.1.50
1.1.40
1.1.30
1.1.20
1.1.10
1.1.01
1.0.500
1.0.491
1.0.480
1.0.470
1.0.461
1.0.451
1.0.440
1.0.430
1.0.421
1.0.410
1.0.400
1.0.390
1.0.381
1.0.370
1.0.360
1.0.350
1.0.340
1.0.331
1.0.320
1.0.310
1.0.300
1.0.290
1.0.280
1.0.270
1.0.260
1.0.250
1.0.240
1.0.230
1.0.220
1.0.210
1.0.200
1.0.190
1.0.180
1.0.170
1.0.160
1.0.151
1.0.141
1.0.130
1.0.120
1.0.110
1.0.100
1.0.90
1.0.80
1.0.70
1.0.60
1.0.50
1.0.41
1.0.30
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i tools-vue3

Weekly Downloads

36

Version

2.0.12

License

MIT

Unpacked Size

17.2 kB

Total Files

10

Last publish

Collaborators

  • a1518079148