【问题标题】:angular2 programmatically import components from resource filesangular2 以编程方式从资源文件中导入组件
【发布时间】: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 最终版本中实现这一点?您能否提供更多信息或分享有助于更好学习的示例?

标签: angular angular2-routing


【解决方案1】:

令人惊讶的问题中的代码有效:D 使用代码并享受动态路线添加

【讨论】:

    猜你喜欢
    • 2015-08-12
    • 1970-01-01
    • 2016-05-01
    • 2016-05-26
    • 1970-01-01
    • 2011-03-25
    • 2015-08-30
    • 2021-04-30
    • 1970-01-01
    相关资源
    最近更新 更多