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);
答案 0 :(得分:0)
令人惊讶的是,问题中的代码有效:D 使用代码并享受动态路线添加