File naming convention for Angular classes

The Angular Style Guide contains naming conventions for common file types like heroes.component.ts

or heroes.service.ts

etc. But what about classes that don't have decorator symbols? For example, the Heroes tutorial creates a Hero class:

export class Hero {
  id: number;
  name: string;
}

      

He then names this file hero.ts

. This naming convention is like directory chaos app

. It seems logical to create a folder with class names (in the application directory) and then add a suffix class.ts

for each file, eg hero.class.ts

.

However, I have not seen anyone use this convention. I have seen people use the suffix model.ts

, but it is unclear if this is best practice and it is not mentioned in the Angular Style Guide .

Can anyone shed some light on this topic? Sorry if I missed something. Thanks in advance.

+3


source to share


2 answers


Angular The style guide explicitly says to use .model

for simple models. However, you should consider 2 things:



  • for enums

    no agreement. Personally, I keep them inside a file service

    or model

    , depending on how it should be used
  • allows you to create others entities

    , so within your project you can add other "types" (recource, config)
  • If the application uses a class / model / interface (for example User

    ), the manual suggests saving it in a shared

    module that is logically correct: SharedModule

    it is supposed to import several times, but CoreModule

    only once (c AppModule

    )
+1


source


There are two ways to create packages (folders) in your application:

  • package by layer
  • by function


Angular prefers "package by function" and why the hero class stays in the hero component it belongs to. A good post on why "package by function" is the preferred way is here .

As for the classes / services that are used for multiple components - Angular StyleGuide suggests using the main package. If it is a component that is generic, then a generic package.

0


source







All Articles