angular - 将字符串类型转换为组件
问题描述
我必须在 Angular 项目中创建路由文件,并且所有路径和组件名称都存储在数据库中。
组件将根据登录用户权限进行选择
现在我想在路由模块文件中创建路由数组。
我将根据用户从数据库中获取所有路由路径并制作路由数组。
但我不知道如何将字符串组件名称转换为组件。
如果有人有想法,请帮忙。
解决方案
You can write a function to return each component type based on the string value, something like this:
convertComponent(componentName: string) {
switch(componentName) {
case 'SomeComponent':
return SomeComponent
break
case 'SomethingelseComponent':
return SomethingelseComponent
break
// etc
}
}
Then call it when you are making your routing module:
{ path: 'something/', component: convertComponent(componentNameString) },
You can also use a registry map instead of the switch case, and set the elements in each component you define.
推荐阅读
- java - 获取 Vaadin 组合框中的所有可用值
- reactjs - 反应、axios 和路由
- java - 如何在没有关键工作volatitle的情况下实现DCL?
- mysql - 从表中获取 WHERE 子句的条件
- kubernetes - 对等集群中的 nginx 入口错误重置连接
- php - ElasticSearch 7 & PHP - 为父/子关系创建映射
- javascript - 无法禁用按钮
- javascript - 在 mongo 的嵌套对象数组中查找和更新值
- python - Matthew 的相关系数和精度在 RandomizedSearchCV 中引发错误
- java - 如何解决 JSP 页面中的这个 XSS 安全问题