Angular 2 : Circular Feature module dependency

后端 未结 1 1030
梦谈多话
梦谈多话 2021-01-12 23:06

I am currently working on one of application of Angular2. I have 3 feature modules in it which contains other sub feature modules. I want to load Sub feature module of Featu

相关标签:
1条回答
  • 2021-01-12 23:51

    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)
    
    0 讨论(0)
提交回复
热议问题