I want to load routes for the @RouteConfig Dynamically from a service which fetches in format JSON,
[
{ "path" : "/about" , "name" : "About" , "component" : "AboutComponent" },
{ "path" : "/contact" , "name" : "Contact" , "component" : "ContactComponent" }
]
Following is the code for pushing it into the RouteDefinition Array,
for (let i = 0; i < data.length; i++) {
//console.log(data[i].path+" "+data[i].name+" "+data[i].component);
this.routeConfigArray.push({ //routeConfigArray : RouteDefinition
'path': data[i].path,
'name': data[i].name,
'component': data[i].component
});
this._router.config(this.routeConfigArray); // THIS FAILS TO CONFIG PATHS ON ROUTER
}
The 'component':data[i].component
requires Classname, where as it recieves a it via a String class. How do i convert the string containing classname into a class??
Also i have tried the Route class using :
this.routeConfigArray.push(new Route({path: data[i].path, name: data[i].name, component:data[i].component}));
Console Error:
Component for route "/about" is not defined, or is not a class. i have tried numerous ways like using
eval("new "+data[i].component+"()); || new window[data[i].component] .
I am stuck at this and really confused as to how to resolve this.