angular2以编程方式从资源文件导入组件 [英] angular2 programmatically import components from resource files
本文介绍了angular2以编程方式从资源文件导入组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Angular2 rc.4-rc.5
Angular2 rc.4 - rc.5
我们可以通过编程方式导入组件吗?
Can we import components programmatically ?
例如,如果我有以下要转换的导入语句
for example if i have following import statements i want to convert
import {HelloComponent} from './hello.component';
import {IncComponent} from './inc.component';
是否可以在运行时使用以下方式从资源文件中使用system.import导入组件?任何建议,我们都会感谢
is it possible to import components on runtime using system.import from the resource file as following ? any suggestion is appreciated
我有一个数组
让路由:RouterConfig = [];
I have an array
let routes: 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);
推荐答案
令人惊讶的是,这些问题中的代码是:D 使用代码并享受动态路线添加
Surprisingly code in the questions worked :D use the code and enjoy dynamic route addings
这篇关于angular2以编程方式从资源文件导入组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文