6

假设我有以下设置:

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 });

员工路由.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
    ]
})

员工模块.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

我受到了欢迎

错误:无法匹配任何路线:'employee/14/sales'

但是,我可以输入

sales

这在它不应该工作的情况下工作,所以不知何故,所有路线都直接连接到/而不是建立在彼此之上。

我错过了什么?

可以在此处找到演示该问题的编辑plnkr 。

4

1 回答 1

4

我最终得到了这个工作。关键思想不是包含employeeRoutesfrom employee.routing.ts(因为这将添加东西 from EmployeeModuleto AppModules 声明并导致另一个错误消息)而是创建另一个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 可以在这里找到。

于 2016-08-18T06:46:09.237 回答