Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best folder structure for ngrx when lazy loading is used Angular 6?

Tags:

I have an Angular 6 app where lazy loading is used. My folder structure looks like this:

src
  app
    main
      products
      invoices
      customers
      suppliers
    core
      header
      footer
      services
      core.module.ts
    shared
    app-routing.module.ts
    app.component.html
    app.component.css
    app.component.spec.ts
    app.component.ts

I followed the recommended folder structure for lazy loading (here, each folder under the main folder is its own module section).

If I want to incorporate ngrx, I have seen differing opinions as to where to include the store, reducers, etc. One article I read said to add a store folder and include all of the ngrx pieces in there. Another article I read said to add the ngrx pieces to each module/section.

There is another stackoverflow article here: What is the best structure for app using ngrx? that talks about ngrx folder structure, but no mention of lazy loading.

Is there a recommended way to structure ngrx when lazy loading is used? Does it make more sense to add a store with reducers, actions, etc. to each module or to make one giant store folder with everything in that? This app is not too big; it's more of a medium sized application.

Thanks!

like image 902
J-man Avatar asked Aug 01 '18 15:08

J-man


People also ask

Where is NgRx data stored?

Where Does NgRx Store Data? NgRx stores the application state in an RxJS observable inside an Angular service called Store. At the same time, this service implements the Observable interface.

What is a good use case for NgRx store?

NgRx is a global state management library that helps decouple the Domain and Business layers from the Rendering layer. It's fully reactive. All changes can be listened to using simple Observables, which makes complex business scenarios easier to handle.

What can I store in NgRx?

NgRx/store is a library for managing state in your Angular applications, it is a reactive state management library powered by RxJS. Similar to Redux, this library can be used to manage the flow of data throughout your application, when actions are dispatched, reducers act on them and mutate the store.


1 Answers

Absolutely add the ngrx pieces to each module separately. It gives you a good structure and every module is self-contained. Furthermore, the ngrx store in each module can be lazy loaded as well, so the state will only be there if you are loading the module.

Further details here: https://medium.com/@AnkurRatra/lazy-loading-of-modules-with-ngrx-store-4th-version-angular-2-23c93295d4e8

like image 140
René Winkler Avatar answered Sep 29 '22 04:09

René Winkler