持久化路由选项卡和子路由

持久化路由选项卡和子路由

本文介绍了Angular2 Routing:持久化路由选项卡和子路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular 社区!

Angular Community!

我目前正在将 AngularJS 应用程序重写为 Angular 2.我想解决可以描述为:路由选项卡 + 子路由的问题.

I'm currently rewriting AngularJS app to Angular 2. I want to solve problem which could be described as: route tabs + child routes.

因此,基本上 Angular 2 中的路由器会破坏非活动组件(我的标签!).问题是我不想要这种行为.原因是我有一些像图表和数据网格这样的组件,想在它们之间快速切换,我不想重新创建它们.

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.

解决方法是:我将一些空组件放到路由中,并自己实例化标签页,用 [hidden] 属性隐藏它们:

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>

推荐答案

我知道您有两个不同的问题:

I understand you have two different questions:

1- 如何防止组件在离开时被破坏.2- 实现子路由.

1- How to prevent the destruction of the component when you leave it.2- implementing child routes.

1) 目前 Angular 没有方便的方法来做到这一点.如果它们是像 canDestroy() 这样的生命周期钩子,我们会很感激.

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) 对于子路由,我只举两个例子:

2) For the child routes I'll just put 2 examples:

Ex1:常规子路由

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:当子路由属于另一个模块时

EX2: When the child routes belongs to another 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' }
  ]
},

`

这篇关于Angular2 Routing:持久化路由选项卡和子路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:16