Angular Community!
I'm currently rewriting AngularJS app to Angular 2. I want to solve problem which could be described as: route tabs + child routes.
So, basically Router in Angular 2 destroys inactive components (my tabs!). The problem is I don't want this behaviour. Reason is I have some components like charts and data grid and want to switch between them fast, I don't want to recreate them.
I have found some workaround to persist my tabs while having routes but using this approach I don't know how to implement child routes. I'd like to also have a solution depth-independent (meaning: working more levels deeper) because I have more subtabs that need to be persisted.
The workaround is: I have put some empty component to routes and instantiate tabs myself hiding them with [hidden]
property:
app.ts:
@Component({ /*...*/ })
@RouteConfig([
{path: '/**', redirectTo: ['Dashboard']},
{path: '/dashboard', name: 'Dashboard', component: EmptyRoute},
{path: '/products/...', name: 'Products', component: EmptyRoute},
{path: '/sales', name: 'Sales', component: EmptyRoute},
{path: '/reports', name: 'Reports', component: EmptyRoute},
])
export class App {
constructor(private router: Router) {
}
public isRouteActive(route) {
return this.router.isRouteActive(this.router.generate(route))
}
}
app.html:
<dashboard [hidden]="!isRouteActive(['/Dashboard'])"></dashboard>
<products-management [hidden]="!isRouteActive(['/Products'])"></products-management>
<sales [hidden]="!isRouteActive(['/Sales'])"></sales>
<reports [hidden]="!isRouteActive(['/Reports'])"></reports>
Angular Router supports multiple outlets in the same application. A component has one associated primary route and can have auxiliary routes. Auxiliary routes enable developers to navigate multiple routes at the same time.
Each route in this array is a JavaScript object that contains two properties. The first property, path , defines the URL path for the route. The second property, component , defines the component Angular should use for the corresponding path.
Routing in Angular allows the users to create a single-page application with multiple views and allows navigation between them. Users can switch between these views without losing the application state and properties.
In Angular, RouterLink is a directive for navigating to a different route declaratively. Router. navigate and Router. navigateByURL are two methods available to the Router class to navigate imperatively in your component classes. Let's explore how to use RouterLink , Router.
I understand you have two different questions:
1- How to prevent the destruction of the component when you leave it. 2- implementing child routes.
1) For now Angular doesn't have convenient way to do this. We would apreciate it if they were a life cycle hook called like canDestroy().
Anyway you can do this either using non routable tabs OR just store your data on a higher component that doesn't get destroyed.
2) For the child routes I'll just put 2 examples:
Ex1: regular child routing
const AdminRoutes: Routes = [
{
path: '',
component: AdminComponent,
children: [
{
path: 'users',
component: UsersComponent,
children: [
{ path: 'acces', component: AccesComponent, data: { preload: true} },
{ path: 'roles', component: RolesComponent, data: { preload: true} },
{ path: '', redirectTo: '/admin/users/acces', pathMatch: 'full' },
{ path: '**', redirectTo: '/admin/users/acces', pathMatch: 'full' },
],
data: { preload: true}
},
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: '**', redirectTo: '/login', pathMatch: 'full' }
]
},
EX2: When the child routes belongs to another module
Code for higher module
`
const appRoutes: Routes = [
{ path: 'login', component: LoginComponent, data: { preload: true} },
{
path: 'admin',
loadChildren: 'app/modules/admin/admin.module#AdminModule',
canActivate: [AuthGuardService],
data: { preload: true}
},
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: '**', redirectTo: '/login', pathMatch: 'full' }
`
Code for child routes in their own module
`
const AdminRoutes: Routes = [
{
path: '',
component: AdminComponent,
children: [
{
path: 'users',
component: UsersComponent,
children: [
{ path: 'acces', component: AccesComponent, data: { preload: true} },
{ path: 'roles', component: RolesComponent, data: { preload: true} },
{ path: '', redirectTo: '/admin/users/acces', pathMatch: 'full' },
{ path: '**', redirectTo: '/admin/users/acces', pathMatch: 'full' },
],
data: { preload: true}
},
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: '**', redirectTo: '/login', pathMatch: 'full' }
]
},
`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With