简体   繁体   English

Angular 2 模块之间的路由

[英]Routing between modules in Angular 2

I'm writing an application where all features got it's own module (A feature could be a page, or a part of a page).我正在编写一个应用程序,其中所有功能都有自己的模块(功能可以是页面,也可以是页面的一部分)。 This because we want all features to have it's own domain logic, services, directives and components, ie in the dashboard module we got an ChartComponent widget that I don't want to expose to other views like login or profile.这是因为我们希望所有功能都有自己的域逻辑、服务、指令和组件,即在仪表板模块中,我们有一个 ChartComponent 小部件,我不想将其公开给其他视图,如登录或配置文件。

The problem is when working with routing in Angular 2 you always routes to a particular component, not a module.问题是在 Angular 2 中使用路由时,您总是路由到特定组件,而不是模块。

In our case, to set up a route for path: '/dashboard' component: DashboardComponent we need to declare DashboardComponent in app.module.ts, and that's fine, but since we're still in the module app.module our CharComponent is not exposed and will not render in our DashboardComponent since it's declared in dashboard.module.ts and not app.module.ts.在我们的例子中,要为 path: '/dashboard' component: DashboardComponent 设置一个路由,我们需要在 app.module.ts 中声明 DashboardComponent,这很好,但由于我们仍在模块 app.module 中,我们的 CharComponent 是未公开且不会在我们的 DashboardComponent 中呈现,因为它是在dashboard.module.ts 中声明的,而不是在 app.module.ts 中声明的。

If we declare ChartComponent in app.module.ts it's working as it should but we lost the architecture for our application.如果我们在 app.module.ts 中声明 ChartComponent,它会正常工作,但我们丢失了应用程序的架构。

The file structure for the application is something like this:应用程序的文件结构是这样的:

└─ src/
   └─ app/
      ├─ app.module.ts
      ├─ app.component.ts
      ├─ app.routing.ts
      ├─ profile/
      |  ├─ profile.module.ts
      |  └─ profile.component.ts
      ├─ login/
      |  ├─ login.module.ts
      |  └─ login.component.ts
      └─ dashboard/
         ├─ dashboard.module.ts
         └─ dashboard.component.ts
            └─ chart/
               └─ chart.component.ts

It's not necessary to import components into main(app) module,不需要将组件导入 main(app) 模块,

If you are loading routes lazily you may just define path like below,如果你懒惰地加载路由,你可以像下面这样定义路径,

// In app module route
{
 path: 'dashboard',
 loadChildren: 'app/dashboard.module#DashboardModule'
}

// in dashboard module
const dashboardRoutes: Routes = [
  { path: '',  component: DashboardComponent }
];

export const dashboardRouting = RouterModule.forChild(dashboardRoutes);

@NgModule({
  imports: [
   dashboardRouting
  ],
  declarations: [
    DashboardComponent
  ]
})
export class DashboardModule {
}

OR

You may just import the DashboardModule in the main module and it will work if the routes are not lazily loaded.您可以只在主模块中导入DashboardModule ,如果没有延迟加载路由,它将起作用。

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    DashboardModule,
    routing
  ],
  declarations: [
    AppComponent
  ],
  providers: [
    appRoutingProviders
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule {
}

事实证明,延迟加载在 RC5 中无法正常工作,只是升级到 RC6 并且一切正常。

Although that logic is nice, i would put all the rights in an accountmanagement component module for future tick on and off on features, while you don't want for your users to have all the rights you might want to upgrade a user account in the future.虽然这个逻辑很好,但我会将所有权限放在帐户管理组件模块中,以便将来在功能上打勾和关闭,而您不希望您的用户拥有您可能想要升级用户帐户的所有权限未来。 Expandability and foresee save's you time from future coding if your system is indeed keen on that logic.如果您的系统确实热衷于该逻辑,则可扩展性和预见性可以为您节省未来编码的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM