在我的angular2应用程序中,我有两个子模块(登录)和(行),路由到登录工作正常,但是,我想用路由参数路由到我的线路模块,例如/line/param1/param2
,但这会打破导航...
我有以下根模块:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
LoginModule,
LineModule,
routing
],
providers: [appRoutingProviders],
bootstrap: [AppComponent]
})
export class AppModule { }
这是我的app.routes.ts
文件:
import { Routes, RouterModule } from '@angular/router';
const appRoutes: Routes = [
{ path: 'login', loadChildren: 'login/login.module', pathMatch: 'prefix'},
{ path: 'line', loadChildren:'line/line.module', pathMatch: 'prefix'}
];
export const appRoutingProviders: any[] = [];
export const routing = RouterModule.forRoot(appRoutes);
现在我的LoginModule工作正常,带有子路由。但是,我遇到了LineModule
的问题。
line.module.ts
:
import { CommonModule } from '@angular/common';
import { MaterialModule } from '@angular/material';
import { LineComponent } from './line.component';
import { lineRouting } from './line.routes';
import { DataService } from './services/data/data.service';
@NgModule({
imports: [
CommonModule,
MaterialModule,
lineRouting
],
providers:[DataService],
declarations: [LineComponent]
})
export class LineModule { }
line.routes.ts
:
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LineComponent } from './line.component';
const lineRoutes: Routes = [{
path: 'line',
component: LineComponent,
children: [
{ path: '/:dep/:id', component: LineComponent }
]
}];
export const lineRouting: ModuleWithProviders = RouterModule.forChild(lineRoutes);
现在路由到http://localhost:4200/line
工作正常(我的服务收到错误,因为dep和id未定义,但组件呈现)。但是,如果我尝试http://localhost:4200/line/finance/line3
,那么我得到:
EXCEPTION:未捕获(承诺):错误:找不到模块'line / line.module'。
错误:找不到模块'line / line.module'
如果未正确定义路线,我会收到这些错误。我尝试更改app.routes.ts
,因此路径为line/:dep/:id
,但我得到了相同的结果。
我做错了什么?
答案 0 :(得分:0)
我通过删除路径中的第一个斜杠解决了类似的问题,
children: [
{ path: ':dep/:id', component: LineComponent }
]