【发布时间】:2017-07-28 15:10:23
【问题描述】:
根据 Angular 文档,路由示例的路由与它尝试路由的模块(AppModule)在同一个模块内完成。像这样:
const appRoutes: Routes = [
{ path: 'crisis-center', component: CrisisListComponent },
{ path: 'hero/:id', component: HeroDetailComponent },
{
path: 'heroes',
component: HeroListComponent,
data: { title: 'Heroes List' }
},
{ path: '',
redirectTo: '/heroes',
pathMatch: 'full'
},
{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
imports: [
RouterModule.forRoot(
appRoutes,
{ enableTracing: true } // <-- debugging purposes only
)
// other imports here
],
...
})
export class AppModule { }
但是,style guide for Angular 提到了路由模块的使用。因此为 AppRoutingModule 添加一个文件并在 AppModule 中导入模块,而不是简单地在 AppModule 中完成路由。根据我从各种教程、指南等中收集到的信息,将使用 AppRoutingModule。
然而,我仍然对应该使用哪种结构感到困惑。我被告知软件结构中的模块应该具有尽可能少的依赖关系,以便它们可以轻松部署和/或重用。但是,不是有一个单独的模块用于 100% 依赖于另一个模块的路由吗?
来自 AppRoutingModule 的路由不适用于 AppModule 之外的任何东西。那么为什么要在路由使用的每个组件上复制导入,而不是仅仅在 AppModule 中创建路由呢?
是否有我应该为我的项目使用的特定结构(以及为什么),还是仅取决于我想要如何构建我的项目的个人偏好?
【问题讨论】:
-
还是只取决于我个人对项目结构的偏好? - 是的
标签: angular module structure modularity