javascript
Unknown variable dynamic import
前端做动态路由权限
component = () => import(`../views/${url}/${url}.vue`)
1
解决方法:
// 首先把你需要动态路由的组件地址全部获取
let modules = import.meta.glob('../views/**/*.vue')
// 然后动态路由的时候这样来取
component = modules[`../views/${url}/${url}.vue`]
自定义案例
navigationList.forEach( navigation => {
let modules = import.meta.glob('../views/**/*.vue')
router.addRoute('index', {
path: navigation.url,
// meta: { name: navigation.name, icon: navigation.icon },
name: navigation.name,
component: modules[`../views${navigation.url}${navigation.url}.vue`],
})
})
console.log(router.getRoutes(), '查看现有路由')
next()
}