问题
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>
回答1:
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' }
]
},
`
来源:https://stackoverflow.com/questions/34925782/angular2-routing-persisting-route-tabs-and-child-routes