Angular 2 : Circular Feature module dependency

人盡茶涼 提交于 2019-12-01 05:42:39

Routes should live in a place separate from the components and outside the modules those components are declared in.

For the longest time, I followed the pattern you're using too. topic-routing.module.ts seems like it should live with the topic components. But recently I've begun thinking about it in a different light, and your conundrum here highlights this perfectly.

I've begun thinking of routes as the heart of a given application. This paradigm shift happened when I began writing a second application and decided to re-use many of the components/modules I had written in the first one. I noticed that the only things that didn't make sense to reuse were the routes.

It was as if the routes defined the "app" and the modules/components are building blocks to be used by any given application.

In that light, I would recommend the following:

Move your route definitions out of each module into the top level app. They could live in a directory next to app.routes, and you could keep them distributed across their current files, or if you don't have that many of them, you could just merge them into the same file.

It may seem counter-intuitive, and you lose the "vertical" grouping where all topic stuff lives with the topics and all the action stuff lives with the actions. But when you look at routes as a fundamentally different animal than the components to which they refer, then it's less painful and it certainly solves your issue.

src
  |-app.component.ts
  |-app.component.html
  |-app.routes.ts  <-- includes the routes in the sibling directory
  |-routing
      |- action.routes.ts
      |- action-detail.routes.ts
      |- topic.routes.ts
      \- decision-topic-detail.ts
  |-decision-topic-detail (module)
  |-topic (module)
  \-action (module)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!