angular2 以编程方式从资源文件中导入组件 [英] angular2 programmatically import components from resource files

查看:29
本文介绍了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 从资源文件中导入组件,如下所示? 任何建议表示赞赏

我有一个数组让路由: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆