I'm currently starting a new project with Angular 2.0 (release Version) and I want to define some global data models/schemas. As I understand it, Angular 2 has no default way of handling pure data classes like this:
export class TestModel {
id: number;
name: string;
randomAttribute: number;
author: string;
}
So my first question regarding best practices is: Should I define such classes when working with Angular 2?
For the design and concept of my whole application I think they are necessary but I'm not sure if I'm applying the wrong way of thinking here.
This data classes are sometimes needed in multiple modules (ngModule) so my second question is Where do I place them in my app? Currently I have the following structure:
/app
/shared
shared.module.ts
test.model.ts
/module1
module1.module.ts
foo.component.ts
[...]
/module2
module2.module.ts
bar.component.ts
[...]
app.module.ts
[...]
My first thought was to include the instruction.model.ts
in the shared.module
and export it in every module that imports shared.module
. That doesn't seem to work because the model is not a directive, pipe or module. Is there a way to export it anyway?
The simpler solution would be to just directly import the test.model.ts
file and every other shared model in every module that needs it. But this seems clunky and not convenient for multiple models.
The third possible solution I thought of was to put all shared data models in a separate folder, bundle their export in a single file like the one below and import this file in every module that needs it.