文章摘要
这篇文章讨论了在Vue动态路由中,动态传递组件变量时可能出现的问题,即页面可能因变量无法识别而显示空白。文章指出,当`item.component`被赋值为一个函数时,直接使用`import`或`require`加载组件文件会导致报错。为了解决这一问题,文章提供了一种解决方案:通过将组件名称传递为变量,并使用`import`函数在组件的`Vue`文件中加载组件。具体代码示例如下: ```javascriptitem.component = (name) => () => import(`@/${name}.vue`);``` 这种解决方案能够正确加载和使用动态传递的组件变量,避免页面出现空白问题。文章还提到了更多相关资料,供进一步学习和参考。
目录menuList 后台数据返回格式路由里面component
{
// 统计分析
path: ‘/statistics’,
name: ‘Statistics’,
meta:{title: ‘数据统计’},
component: ‘views/statistics/Statistics’
}
// 统计分析
path: ‘/statistics’,
name: ‘Statistics’,
meta:{title: ‘数据统计’},
component: ‘views/statistics/Statistics’
}
接收的是 能正常显示页面


如果动态参数,以下三种都会报错,页面空白
let cop=`@/${item.component}`
// item.component=()=> import(cop)
// item.component=resolve=> require([cop], resolve)
// item.component=Promise.resolve().then(()=> require(`@/${item.component}`))
// item.component=()=> import(cop)
// item.component=resolve=> require([cop], resolve)
// item.component=Promise.resolve().then(()=> require(`@/${item.component}`))

请问有没有好的办法解决这个问题?
name为组件页面地址
item.component=(name)=> ()=> import(`@/${name}.vue`)
以上就是vue 动态路由component 传递变量报错问题解决的详细内容,更多关于vue 动态路由传递变量报错的资料请关注脚本之家其它相关文章!
您可能感兴趣的文章:vue-router4动态路由刷新404/白屏的解决Vue router动态路由实现过程Vue实现动态路由导航的示例vue实现动态路由添加功能的简单方法(无废话版本)Vue动态路由路径重复及刷新丢失页面问题的解决Vue中如何实现动态路由的示例代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。


