2

I’m studying angular’s style guide and I got some doubts while developing a simple app! The app is to control user’s stock exchange shares. So it’s needed:

  • a module to authenticate user (signin, signup and reset password)
  • a module with a component to show all user’s stocks and another one to add new stocks
  • a module to edit user’s profile

Authentication module will have a different layout from the rest of app (authentication will have just a centralised form while rest of app will have a nav-bar as menu).

Question 1: I’ll create a component called Layout inside Auth module and use it only for authentication views; Where do I create the Layout component or module (I don’t know which one) to use with the rest of app? Should I create a Core module and inside this module a Layout module? Should I create a core module and a layout module (outside of core)?

Question 2: where should I create the guard for authentication? This guard will be used for Stocks and Profile modules to avoid users that aren’t logged in

Question 3: how would you structure this simple project regarding folders and files?

1 Answers1

2

Currently, I'm using this article to organize my Angular folder structure. Basically, we have three main folders - core, shared, views.

  • core: it has the CoreModule which provides all app common services. It's imported only once in AppModule. In this folder, I have services and guards subfolders because they are injectables (question 2 answer). guards I don't provide at CoreModule, I provide them at routing modules, i.e AppRoutingModule.
  • shared: it has the SharedModule which declare and exports all app common components, pipes, enums, classes and directives (all classes expect services and guards). SharedModule is imported by views module.
  • views: it gets all views modules (generally lazy-load modules).

The structure is (question 3 answer):

/app
|---/core
|   |---/guards
|   |---/services
|   |---core.module.ts
|
|---/shared
|   |---/components
|   |---/directives
|   ...
|   |---shared.module.ts
|
|---/views
|   |---/view-X
|       |---/components
|       |---/directives
|       |---view-X.component.html
|       |---view-X.component.css
|       |---view-X.component.spec.ts
|       |---view-X.component.ts
|       |---view-X.module.ts
|       |---view-X-routing.module.ts

In your case, could have three views folders/modules: authenticate, user-stock and user-edit. Each one will have the structure similar to the view-X folder example (question 1 answer).

Pedro Arantes
  • 5,113
  • 5
  • 25
  • 60
  • Hey! Thanks for answering!! I liked your way of organizing the project, but I got two questions: **1-** would I create the Layouts (for authentication and app) inside shared folder?! **2-** in app/core/services are the services that communicate with API? – Murilo Boareto Delefrate Feb 15 '18 at 11:43
  • **1-** It depends. If the layout is used only for authentication, I'd create in /views/authentication. If it is used for other modules (user-edit and user-stock), I'd create at /app/shared because them can import `SharedModule` and have access to the common layout. **2-** `CoreModule` has all common services, like API service. `CoreModule` is provided ONLY in `AppModule` to inject the services only once. – Pedro Arantes Feb 15 '18 at 12:57
  • Got it!! Not sure how the project would look like when it gets bigger, but for now, I think it's clean and easy enough to implement new features!! Thanks Pedro!! – Murilo Boareto Delefrate Feb 15 '18 at 13:23
  • @MuriloBoaretoDelefrate, you're welcome. Folder and project structure is a subject I have a lot of interest and I'd like to ask you if you discover another way to structure an Angular project, share with us :) – Pedro Arantes Feb 15 '18 at 13:27
  • Sure!! As soon as I finish the project, I'll update my question with the structure I ended up with! – Murilo Boareto Delefrate Feb 15 '18 at 13:59