自动读取plugins目录下所有插件的pages的目录下的*.vue 并加入路由

This commit is contained in:
hawk86104 2023-10-21 18:51:00 +08:00
parent df5960fd16
commit 3130cb870f
5 changed files with 78 additions and 6 deletions

View File

@ -4,7 +4,7 @@
* @Autor: Hawk
* @Date: 2023-10-16 10:53:09
* @LastEditors: Hawk
* @LastEditTime: 2023-10-17 15:36:11
* @LastEditTime: 2023-10-21 18:48:57
*/
// import { resolve } from 'path';
import { join } from 'path';

View File

@ -1,3 +1,11 @@
/*
* @Description:
* @Version: 1.668
* @Autor: Hawk
* @Date: 2023-10-16 10:53:09
* @LastEditors: Hawk
* @LastEditTime: 2023-10-21 18:50:15
*/
import { access, defineRuntimeConfig } from '@fesjs/fes';
import PageLoading from '@/components/pageLoading.vue';
@ -6,7 +14,7 @@ import UserCenter from '@/components/userCenter.vue';
export default defineRuntimeConfig({
beforeRender: {
loading: <PageLoading />,
action() {
action () {
const { setRole } = access;
return new Promise((resolve) => {
setTimeout(() => {
@ -23,3 +31,55 @@ export default defineRuntimeConfig({
renderCustom: () => <UserCenter />,
},
});
const findStringBetween = (str) => {
const regex = /plugins\/([^/]+)\/pages\//;
const match = str.match(regex);
if (match && match[1]) {
return match[1];
}
return null;
}
//pluginspages*.vue
export function patchRoutes ({ routes }) {
const needAddRouter = {}
const viteModules = import.meta.glob('./plugins/*/pages/*.vue');
for (const [key, value] of Object.entries(viteModules)) {
const pluginName = findStringBetween(key)
const pageName = key.slice(key.lastIndexOf('/') + 1, key.lastIndexOf('.'))
if(pluginName){
const nodeRouter = {
path:`/${pluginName}/${pageName}`,
component:value
}
if(needAddRouter[pluginName]){
needAddRouter[pluginName].push(nodeRouter)
}else{
needAddRouter[pluginName] = [nodeRouter]
}
}
}
// eslint-disable-next-line guard-for-in
for (const one in needAddRouter) {
const OneNode = needAddRouter[one]
const addRouterNode = {
path: `/${one}`,
component: () => import("./components/suspenseLayout.vue"),
children: []
}
// eslint-disable-next-line array-callback-return
OneNode.every((e)=>{
addRouterNode.children.push(e)
})
routes.unshift(addRouterNode)
}
// routes.unshift({
// path: "/digitalCity",
// component: () => import("./components/suspenseLayout.vue"),
// children: [{
// path: "/digitalCity/city",
// component: ()=>import('./plugins/digitalCity/pages/city.vue'),
// }]
// });
}

View File

@ -0,0 +1,13 @@
<!--
* @Description:
* @Version: 1.668
* @Autor: Hawk
* @Date: 2023-10-21 17:24:44
* @LastEditors: Hawk
* @LastEditTime: 2023-10-21 17:24:52
-->
<template>
<Suspense>
<router-view />
</Suspense>
</template>

View File

@ -4,7 +4,7 @@
* @Autor: Hawk
* @Date: 2023-10-17 09:35:18
* @LastEditors: Hawk
* @LastEditTime: 2023-10-20 17:25:16
* @LastEditTime: 2023-10-21 16:37:22
-->
<script setup lang="ts">
import { useRenderLoop } from '@tresjs/core'
@ -21,7 +21,6 @@ const props = withDefaults(defineProps<{
landColor: '#112233',
opacity: 0.9
})
debugger
const timeDelta = { value: 0 }
const CITY_UNTRIANGULATED = props.model.city
const LANDMASS = props.model.land

View File

@ -4,10 +4,10 @@
* @Autor: Hawk
* @Date: 2023-10-17 08:30:49
* @LastEditors: Hawk
* @LastEditTime: 2023-10-20 17:16:04
* @LastEditTime: 2023-10-21 17:22:46
-->
<template>
<TresCanvas v-bind="state">
<TresCanvas v-bind="state" window-size>
<TresPerspectiveCamera :position="[600, 750, -1221]" :fov="45" :near="1" :far="10000" />
<OrbitControls v-bind="controlsState" />
<TresAmbientLight color="#ffffff" />