【发布时间】:2016-08-09 20:42:40
【问题描述】:
Angular2 rc.4 - rc.5
我们可以通过编程方式导入组件吗?
例如,如果我有以下要转换的导入语句
import {HelloComponent} from './hello.component';
import {IncComponent} from './inc.component';
是否可以在运行时使用 system.import 从资源文件中导入组件,如下所示?任何建议表示赞赏
我有一个数组
让路线:RouterConfig = [];
let routeArr =[
{path: 'hello', component:'HelloComponent', resource:'./hello.component'},
{path:'inc', component:'IncComponent', resource:'./inc.component'}
];
// injecting router object
constructor(private router: Router){
}
// iterating routeArr and creating RouteConfig objects
routeArr.forEach((route : any) => {
// import components programmatically on run time ?
System.import(route.resource).then(
m => {
// create route config object and add in routes object
routes.push({
path: route.path
component: m[route.component]
});
}
)
});
// load all routes which we just created
router.resetConfig(routes);
【问题讨论】:
-
我们如何在 app.routing Angular 最终版本中实现这一点?您能否提供更多信息或分享有助于更好学习的示例?