我在angular2中有根模块和子模块,都有自己定义的路线。 在子模块正在配置RouterModule作为Angular2儿童路线
RouterModule.forChild(ROUTES)
在根模块(父),我配置RouterModule作为
RouterModule.forRoot(ROUTES)
我在这两个孩子和根使用相同路线名称
儿童
export const ROUTES: Routes = [
{ path: '', component: HomeComponent }, //outputs I am child
{ path: 'home', component: HomeComponent }
];
在根(父)
export const ROUTES: Routes = [
{ path: '', component: HomeComponent }, //outputs I am parent
{ path: 'home', component: HomeComponent }
];
我导入子单元中的根模块
import .....
import {AppModule as ChildModule} from 'child-module/src/app/app.module';
@NgModule({
bootstrap: [ AppComponent ],
declarations: [
AppComponent,
HomeComponent
],
imports: [ // import Angular's modules
BrowserModule,
FormsModule,
HttpModule,
ChildModule,
RouterModule.forRoot(ROUTES, { useHash: true, preloadingStrategy: PreloadAllModules })
],
providers: [
]
})
export class AppModule {
}
当我打开我的组件(我的意思是根模块),默认路由总是要孩子模块,而不是rootmodule即它的打印“我是孩子”,在我期望“我是父母”的情况下,只有在加载子路径时才应该加载子路径,我如何将其路由到根(父)模块的默认路径而不是子模块?
你有没有考虑过在父路径中加入延迟加载的子路径,比如'{path:child,loadChildren:'子模块的路径}}?你可以在这里阅读更多关于它的信息(https://angular.io/docs/ts/latest/guide/router.html) –
@Madhu Ranjan,问题是孩子(默认)路由默认加载,即使没有配置我想避免 –
您是否在子模块中配置了子路由?如果是的话,那么当你在应用模块中导入子模块时,子模块的路由将被添加.. –