Angular 2中模块之间的路由 [英] Routing between modules in Angular 2

查看:194
本文介绍了Angular 2中模块之间的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,其中所有功能都有其自己的模块(功能可以是页面,也可以是页面的一部分).这是因为我们希望所有功能都具有自己的域逻辑,服务,指令和组件,即在仪表板模块中,我们获得了一个ChartComponent小部件,我不想将其公开给其他视图,例如登录名或个人资料.

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, i.e. in the dashboard module we got an ChartComponent widget that I don't want to expose to other views like login or profile.

问题是在Angular 2中使用路由时,您总是路由到特定组件,而不是模块.

The problem is when working with routing in Angular 2 you always routes to a particular component, not a module.

在我们的例子中,要为路径:'/dashboard'组件:DashboardComponent设置路由,我们需要在app.module.ts中声明DashboardComponent,这很好,但是因为我们仍然在app.module模块中我们的CharComponent没有公开,也不会在DashboardComponent中呈现,因为它是在dashboard.module.ts中声明的,而不是在app.module.ts中声明的.

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.

如果我们在app.module.ts中声明ChartComponent可以正常工作,但会丢失应用程序的体系结构.

If we declare ChartComponent in app.module.ts it's working as it should but we lost the architecture for our application.

应用程序的文件结构如下:

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

推荐答案

事实证明,延迟加载在RC5中不能正常工作,只是升级到RC6才可以.

It turns out that lazy loading didn't work properly in RC5, just upgraded to RC6 and it all worked.

这篇关于Angular 2中模块之间的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆