在Angular 2 RC5 / Router 3 RC1中路由嵌套模块

时间:2016-08-16 09:37:00

标签: angular angular2-routing angular2-router3

我们说我有以下设置:

employee -------+ employee.module.ts
                | employee.routing.ts
                + employee.component.ts
                |
sales ----------+ sales.module.ts
                | sales.routing.ts
                + sales.component.ts
app.module.ts
app.routing.ts
app.component.ts

我希望我的路线看起来像

employee/14/sales

所以我继续定义这些路由声明:

app.routing.ts

...
import { AppComponent } from './app.component';

const appRoutes: Routes = [
    { path: '', component: AppComponent }
];

export const appRoutingProviders: any[] = [];
export const routing = RouterModule.forRoot(appRoutes, { useHash: true });

employee.routing.ts

...
import { EmployeeComponent } from './employee.component';

export const employeeRoutes: Routes = [
    { path: 'employee/:id', component: EmployeeComponent }  
];

export const employeeRouting = RouterModule.forChild(employeeRoutes);

sales.routing.ts

...
import { SalesComponent } from './sales.component';

export const salesRoutes: Routes = [
    { path: 'sales', component: SalesComponent }  
];

export const salesRouting = RouterModule.forChild(salesRoutes);

虽然我的模块采用这种形式:

app.module.ts

import { EmployeeModule } from './employee/employee.module';
import { AppComponent } from './app.component';

import {
    routing,
    appRoutingProviders
} from './app.routing';

@NgModule({
    imports: [
        ...
        EmployeeModule,
        routing
    ],
    declarations: [
        AppComponent
    ],
    bootstrap: [
        AppComponent
    ],
    providers: [
        appRoutingProviders
    ]
})

employee.module.ts

import { SalesModule } from '../sales/sales.module';
import { EmployeeComponent } from './employee.component';
import { employeeRouting } from './employee.routing';

@NgModule({
    imports: [
        SalesModule,
        employeeRouting
    ],
    declarations: [
        EmployeeComponent
    ]
})

sales.module.ts

import { SalesComponent } from './sales.component';
import { salesRouting } from './sales.routing';

@NgModule({
    imports: [
        salesRouting
    ],
    declarations: [
        SalesComponent
    ]
})
export class SalesModule {}

我现在可以转到

employee/14

但如果我尝试导航到

employee/14/sales

我和

打招呼
  

错误:无法匹配任何路线:'员工/ 14 /销售'

但是,我可以输入

sales

并且它不起作用,所以所有路线都以某种方式直接连接到/而不是彼此叠加。

我错过了什么?

编辑 plnkr可以找到问题here

1 个答案:

答案 0 :(得分:4)

我最终得到了这个。关键的想法是不要在employeeRoutes中加入employee.routing.ts(因为这会将EmployeeModule的内容添加到AppModule的声明中并导致另一条错误消息)而是创建导航到以“

”开头的路线时,employeeRoutes内的另一个app.routing.ts会延迟加载EmployeeModule
/employee

以下是相关代码:

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

import { AppComponent } from './app.component';

const employeeRoutes: Routes = [
  {
    path: 'employee',
    loadChildren: 'app/employee/employee.module#EmployeeModule'
  }
];

const appRoutes: Routes = [
    { path: '', redirectTo: 'welcome', pathMatch: 'full' },
    ...employeeRoutes
];

export const appRoutingProviders: any[] = [];
export const routing = RouterModule.forRoot(appRoutes, { useHash: true });

可以找到完整的plnkr here