Angularjs2中根模块导入特性模块,如何将特性模块的路由作为根模块路由的子路由导入?
最近在开发时遇到的问题,根模块在导入子模块时,子模块路由会扩展根模块路由。
但是根模块路由和子模块路由是平级关系。
以下是根模块路由
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AuthGuard } from './common/auth/auth.service';
// 根路由器
const manage_routes: Routes = [
{
path: 'manage',
children: [
{ path: '', redirectTo: 'dashboard_conf', pathMatch: 'full' },
...
]
}
];
@NgModule({
imports: [ RouterModule.forChild(manage_routes) ],
exports: [ RouterModule ]
})
export class ManageRoutingModule {}
以下是特性模块路由
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ImageUploadComponent } from './upload_image.component';
import { ImageConfComponent } from './image.component';
import { ImageSeriesConfComponent, ImageSeriesConfDetailComponent, ImageSeriesSetComponent } from './image_series.component';
import { ImageTagConfComponent, ImageTagConfDetailComponent, ImageTagSetComponent } from './image_tag.component';
import { ImageSeriesCategoryConfComponent, ImageSeriesCategoryConfDetailComponent, ImageSeriesCategoryConfSetDetailComponent} from './image_series_category.component';
import { ImageRecommendTagConfComponent, ImageRecommendTagConfDetailComponent } from './image_recommend_tag.component';
// 图片模块路由器
const image_routes: Routes = [
{ path: 'manage/image_upload', component: ImageUploadComponent },
{ path: 'manage/image_conf', component: ImageConfComponent },
{
path: 'manage/image_series_conf',
children: [
{ path: '', component: ImageSeriesConfComponent },
{ path: 'detail/:id', component: ImageSeriesConfDetailComponent },
{ path: 'detail/add', component: ImageSeriesConfDetailComponent },
{ path: 'set/:id', component: ImageSeriesSetComponent },
]
},
...
];
@NgModule({
imports: [ RouterModule.forChild(image_routes) ],
exports: [ RouterModule ]
})
export class ImageRoutingModule {}
根模块导入特性模块
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
...
import { ImageModule } from './image/image.module';
@NgModule({
declarations: [
DashboardConfComponent,
],
imports: [
...
ImageModule, // 图片模块
ManageRoutingModule, // 放在最后
],
providers: [
...
]
})
export class ManageModule {
}
最后得到的路由列表
Routes: [
{
"path": "manage/image_series_conf",
"children": [
{
"path": ""
},
{
"path": "detail/:id"
},
{
"path": "detail/add"
},
{
"path": "set/:id"
}
]
},
{
"path": "",
"redirectTo": "manage",
"pathMatch": "full"
}
]
这两个路由列表合成之后,依然是平级关系,怎么能让特性模块作为根模块的子路由呢?难道只能把所有路由都写在一块吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用懒加载,无需导入特性模块,如下:
根路由: