网站搜索

typescript - 如何重用 Angular 2 路由?


我有一个模块化的 Angular 2 应用,在每个模块中定义了子路由,主路由文件如下:

import { Routes, RouterModule } from '@angular/router';
import { ModuleWithProviders } from '@angular/core';

const appRoutes: Routes = [
    {
        path: 'a', loadChildren: './src/a.module#AModule'
    },
    {
        path: 'b', loadChildren: './src/b.module#BModule'
    },
    { path: '', redirectTo: '/a/1', pathMatch: 'full' },
    { path: '**', redirectTo: '/a/1' }
];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

问题是,我在A中有一个组件需要使用B中的一个组件,在AComponent中我导航到BComponent,我在B中完成需要做的事情后返回AComponent,问题是我的AComponent被重新创建,表单中填写的所有数据都丢失了。

我尝试使用 RouteReuseStrategy,但没有正常工作,在这个 Plunker 中,RouteReuseStrategy 的示例被注释了,要启用它,只需取消注释 app.ts 中的代码。

有人知道我如何在不丢失之前路线数据且不使用 ui-router 的情况下导航吗?