I am learning Angular 2 and I am using angular-cli to generate components and services. I am using the directory structure proposed by angular-cli (see screenshot below). I need to add some domain model object (e.g. User, Profile, etc.), but I don't know where to put them.
What is the recommended location for those objects in the directory structure?
You will want to have your app-wide models stored at the root level (default: src/app
) except that those values should be in the shared
directory (src/app/shared
).
If you generate those models with the CLI (from the project root directory) you will run:
ng generate class shared/profile model
which will yield:
src/app/shared/profile.model.ts
src/app/shared/profile.model.spec.ts
and also add a reference to the profile
exports to the index.ts
within the shared directory, so you can reference them (from the root component)
import { Profile } from './shared';
If the domain model classes are used by all the components, it can be put in a model.ts file within shared folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With