Currently, I am utilizing Angular2 for the frontend of my project and I am faced with the task of registering new Routes from an array.
Within my application, there is a service that retrieves data from a server. This data is then stored in a variable within my AppComponent.
The following function calls my Service to get the Spaces data:
getSpaces() {
this.spaceService.getData()
.then(data => {
this.spaces = data;
for (var i = 0; i < this.spaces.length; i++) {
var item = { label: this.spaces[i].name, icon: 'fa-list', routerLink: ['/database' + this.spaces[i].name] }
this.itempanel[1].items[1].items.push(item);
}
});
}
At the moment, I am adding the name properties from "spaces" to my panelmenu using Primeng. I am assigning a routerLink for each spaces.name, but these routes are not yet included in my @Routes decorator.
My main question is: How can I dynamically register my Routes? I envision them to have a structure similar to this:
new Route {path: 'database'+this.spaces[i].name, component: SpaceComponent}
I attempted to find relevant information in the documentation, however, it seems I might have overlooked something. If you have any suggestions or ideas on how to proceed, please do share.
I understand that Routes can be registered with a param:
@Routes([
{path:'database/:spacename', component: SpaceComponent}
])
However, I am unsure about how to utilize this feature as I am still learning Angular2 (and programming in general). Could you kindly provide guidance on working with route Params (for example, how to incorporate spacename into the link) or advise me on how to register new routes programmatically? Thank you!