vite,动态路由失败报错Failed to fetch dynamically imported module: http://xxxxx/@/views/app/xxxxxxx

vite动态添加路由(打包)

开发环境没有错,在生产上报错** Failed to fetch dynamically imported module: http://xxxxx/@/views/app/xxxxxxx**

1、动态路由添加后,vite 打包后报错

const handleHome = (data)=>{
	const routes = [] 
	for(const item of data){
		const route = {
			const importPromise = import(/* @vite-ignore */ `/@/${item.component}`)
			return importPromise.then((module)=>{
				const Component = module.default ||module
				Component.query = item.query
				return Component
			})
		}
		if(item.children && item.children.length > 0){
			route.children = handleHome(item.children)
		}
		routes.push(route)
	}
	return routes
}

2、defineAsyncComponent组件的方式,vite 打包报错

import { defineAsyncComponent } from 'vue'
const AsyncFooWithOption = (path) =>defineAsyncComponent{
	loader:()=>import ('../../${path}'),
	delay:2000,
	timeout:3000
}
....格式化数据方法省略....
component:AsyncFooWithOption(item.component)

3、第三种写法,找出目录下的所有的.vue文件,与返回的路由地址做匹配,vite 打包后,完美解决

 const importRoute = import.meta.glob('../../views/**/*.vue');
 component: importRoute[`../../${item.component}`],
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值