Angular2路由:持久路由选项卡和子路由

Nam*_*mek 20 angular2-routing angular

角社区!

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

所以,基本上Angular 2中的Router会破坏非活动组件(我的标签!).问题是我不想要这种行为.原因是我有一些组件,如图表和数据网格,并希望快速切换它们,我不想重新创建它们.

我找到了一些解决方法来保留我的标签,同时有路由但使用这种方法我不知道如何实现子路由.我还想要一个独立于深度的解决方案(意思是:更深层次地工作)因为我有更多需要持久化的子标签.

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

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))
    }
}
Run Code Online (Sandbox Code Playgroud)

app.html:

<dashboard [hidden]="!isRouteActive(['/Dashboard'])"></dashboard>
<products-management [hidden]="!isRouteActive(['/Products'])"></products-management>
<sales [hidden]="!isRouteActive(['/Sales'])"></sales>
<reports [hidden]="!isRouteActive(['/Reports'])"></reports>
Run Code Online (Sandbox Code Playgroud)

Tah*_*ued 1

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

1- 如何防止组件在离开时被损坏。2-实施子路线。

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

无论如何,您可以使用不可路由的选项卡来执行此操作,或者仅将数据存储在不会被破坏的更高组件上。

2)对于子路线,我只举两个例子:

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' }
  ]
},
Run Code Online (Sandbox Code Playgroud)

EX2:当子路由属于另一个模块时

更高模块的代码

`

    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' }
Run Code Online (Sandbox Code Playgroud)

`

子路由在自己的模块中的代码

`

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' }
  ]
},
Run Code Online (Sandbox Code Playgroud)

`