Angular2 Child Routes

2
const itemRoutes: Routes = [
{
    path: '', component: ItensComponent,
    children: [
        { path: '', redirectTo: 'lista', pathMatch: 'full' },
        { path: 'lista', component: ListaItensComponent },
        { path: 'novo', component: NovoItemComponent },
        { path: 'edita/:id', component: EditaItemComponent }
    ]
}

];

ListComponent

 editaItem(id: string) {
  this.router.navigate(['../edita', id], {relativeTo: this.route});

}

How can I return to ListItemsComponent after editing the item ??

    
asked by anonymous 10.01.2017 / 03:08

2 answers

1

You can return by using the command history.back(); that I believe is not the best way, or using this._router.navigate(['../'], { relativeTo: this._routeParams }); which in my case works, however my route configuration is different from yours.

export const routing: ModuleWithProviders = RouterModule.forChild([
    { path: '', component: MarcaListComponent },
    { path: ':id', component: MarcaFormComponent }
]);

To add a new item I use the editing route, but passing an id equal to zero. Here are two methods (add, edit):

add() {
    if (this.enableAdd) {
        this._router.navigate([0], { relativeTo: this._route });
    }
}

edit() {
    if (this.enableEdit) {
        this._router.navigate([this.selectedItem.id], { relativeTo: this._route });
    }
}
    
20.01.2017 / 12:31
0

From what I understand you want to access the object you want to edit in the component EditaItemComponent. You through the route you can not send objects to other components, so the easiest way is to use a service that has your list of objects and when you want to edit an object you use the route to pass the id and then in the component EditaItemComponent you access this service and you get the object that matches the id for editing.

    
12.01.2017 / 15:31