angular2 programmatically import components from r

2019-04-15 04:54发布

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';

is it possible to import components on runtime using system.import from the resource file as following ? any suggestion is appreciated

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);

1条回答
女痞
2楼-- · 2019-04-15 05:24

Surprisingly code in the questions worked :D use the code and enjoy dynamic route addings

查看更多
登录 后发表回答