Vue3中vite动态import引⼊打包报错解决 Vite ⽀持使⽤特殊的 a.glob 函数从⽂件系统导⼊多个模块,需要使⽤这个⽅式
推荐几款家用吸尘器import {
RouteRecordRaw,
createRouter,
createWebHistory,
useRoute,
} from "vue-router"
// @ts-ignore
import util from "/@libs/util"
// import Layout from '/@views/layout/Layout.vue'
// @ts-ignore
import store from "/@store/index"
import Layout from "../layout/index.vue"
// 进度条
// @ts-ignore
import NProgress from "nprogress"
import "nprogress/nprogress.css"
import { useStore } from "vuex"
//Vite ⽀持使⽤特殊的 a.glob 函数从⽂件系统导⼊多个模块
专升本第一学历是本科还是专科const modules = a.glob("../views/**/**.vue")
const routes: RouteRecordRaw[] = [
//路由⼀定要与modules导⼊的⼀致(const modules =a.glob("../views/**/**.vue"))
{
path: "/login",
name: "login",
component: modules["../views/system/login/index.vue"],//使⽤../views/
},
// 后台⾸页
{
path: "/",
name: "home",
component: Layout,
redirect: { path: "/index" },
meta: {
title: "⾸页",
},
钥匙的主人children: []
]
const router = createRouter({
history: createWebHistory(),
routes,
})
//处理动态路由因为vue3没有addRoutes 只能遍历⼀个⼀个加
export const routerPackag = function (routers: any) {
if (routers) {
routers.filter((itemRouter: any) => {
if (itemRouterponent != "Layout") {
router.addRoute("home", {
path: `${itemRouter.path}`,
name: itemRouter.name,
meta: {
title: itemRouter.name,
},
component:
//需要⽤vite规定的导⼊⽅式导⼊,否则打包后部署到服务器报错不到动态导⼊的⽂件,
/
/对应上⽅的const modules = a.glob("../views/**/**.vue")
//使⽤/* @vite-ignore */则不会在开发是报错
//使⽤/* @vite-ignore */则不会在开发是报错
modules[/* @vite-ignore */`../views/${itemRouterponent}`], })
}
if (itemRouter.children && itemRouter.children.length) {
routerPackag(itemRouter.children)
}休闲服装搭配图片
return true
})
}
混混王妃傻王爷}
//动态加路由
let allRouter: any = Item("info")
if (JSON.parse(allRouter)) {
routerPackag(JSON.parse(allRouter).routerList)
}
//路由守卫
router.beforeEach(async (to, from, next) => {
const info = await store.dispatch("sys/user/load")
const token: any = ken ? ken : ""
NProgress.start()
/
/ 关闭搜索⾯板
if (to.path == "/login") {
next()
} else {
if (token && token !== "undefined") {
console.log(token, "token")
next()
} else {
// 没有登录的时候跳转到登录界⾯
// 携带上登陆成功之后需要跳转的页⾯完整路径
next("/login")
/
/ 关闭搜索⾯板
NProgress.done()
}
}
})
我是特种兵之火凤凰演员表router.afterEach((to) => {
// 进度条
NProgress.done()
// 更改标题
util.a.title)
})
export default router
核⼼:
//vite的引⼊⽅式
const modules = a.glob("../views/**/**.vue")
//"home"加到home的children中
router.addRoute("home", {
path: `${itemRouter.path}`,
name: itemRouter.name,
meta: {
title: itemRouter.name,
},
component:
modules[/* @vite-ignore */`../views/${itemRouterponent}`], })
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论