I am currently working on dynamically loading routes for the @RouteConfig from a service that fetches data in JSON format.
[
{ "path" : "/about" , "name" : "About" , "component" : "AboutComponent" },
{ "path" : "/contact" , "name" : "Contact" , "component" : "ContactComponent" }
]
Below is the snippet of code I use to populate the RouteDefinition Array,
for (let i = 0; i < data.length; i++) {
this.routeConfigArray.push({
'path': data[i].path,
'name': data[i].name,
'component': data[i].component
});
this._router.config(this.routeConfigArray);
}
The issue arises when trying to set the component property with a string value, which should actually be a Classname. How can I convert the string containing classname into a class?
I have also attempted to use the Route class like this:
this.routeConfigArray.push(new Route({path: data[i].path, name: data[i].name, component:data[i].component}));
However, I keep encountering this Console Error:
Component for route "/about" is not defined, or is not a class. I have tried different approaches such as using
eval("new "+data[i].component+"()); || new window[data[i].component] .
I am facing difficulties in resolving this issue and would appreciate any guidance to solve it.