// route/index.js
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
// const comp = () => import ('../views/AboutView.vue' )
// console.log('comp :>> ', await comp()?.default)
const rote = [
{
path: '/',
name: 'home',
component: HomeView
},
{
path: '/about',
name: 'about',
component: () => import('../views/AboutView.vue')
}
]
console.log('rote :>> ', rote);
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: rote
})
const page = import.meta.glob('../views/**/*.vue', { eager: true })
const cru = Object.entries(page)
const loadRoute = (list) => {
list.forEach((item) => {
const path = item.site
const pathUrl = `/${path}`
console.log('path :>> ', path);
cru.forEach( async cruItem => {
if (cruItem[0] === path) {
item.component = await cruItem[1]?.default
console.log('cruItem[1] :>> ', cruItem[1]);
console.log('11111 :>> ', 11111);
console.log('path :>> ', path);
console.log('11111 :>> ', 11111)
}
})
if (page[pathUrl]) {
// console.log('1111111 :>> ', 1111111)
// console.log('page[pathUrl] :>> ', page[pathUrl])
// console.log('1111111 :>> ', 1111111)
/* @vite-ignore */
// item.component = () => import( page[pathUrl] )
item.comp2 = 2222222
}
router.addRoute(item)
if (item.children) {
loadRoute(item.children)
}
})
}
console.log('page :>> ', page);
const asyncRoute = [
{
path: '/layout',
name: 'layout',
site: '../views/Layout.vue',
redirect: '/demo1',
component: () => import('../views/Layout.vue'),
children: [
{
path: '/demo1',
name: 'demo1',
site: '../views/Demo1.vue',
component: () => import('../views/Demo2.vue')
},
{
path: '/test1',
name: 'test1',
site: '../views/test/test1.vue',
component: () => import('../views/text/test1.vue')
},
{
path: '/test2',
name: 'test2',
site: '../views/test/test2.vue',
component: () => import('../views/test/test2.vue')
}
]
}
]
router.beforeEach((to, from, next) => {
if (!router.hasRoute('layout')) {
loadRoute(asyncRoute)
// console.log('asyncRoute :>> ', asyncRoute);
router.replace({...to,replace:true})
}
console.log('router.getRoutes() :>> ', router.getRoutes())
next()
})
export default router
标签:vue,console,log,..,views,vite,vue3,path,路由
From: https://www.cnblogs.com/hjk1124/p/18393179