r/Angular2 • u/FewDot9181 • Mar 24 '26
Naming files in Angular
If I have a service file under the services folder and then under the api folder because it's a file for calling apis. Would a good name for the file for example UserApiService or would just User be fine?
2
Upvotes
5
u/jessycormier Mar 24 '26
Sounds like you need to decide and stay consistent in your pattern.
Personally I like the original naming angular used so user-api.service.ts The class would be UserApiService.
I would consider options of where to store api based services and related files (interfaces, types, dto)
My reasoning for liking a full name in the class and file is purely for ease of understanding why I'm looking at. If you have a user api, user service, user interface, what do you import in your user component?
4
u/imsexc Mar 24 '26
If you embrace the new approach for a while, you'll know how problematic it is due to name collissions.
Can't pool all of them ( files of the same name but different purpose, e.g pipe, component, directive, service) in a single folder.
Utilizing IDE's auto import became a pain, that if we want to import a service we have to make sure we import the service, and not the component. Vice versa
1
u/Burgess237 Mar 25 '26
To answer your question: Yes.
The longer answer here is: You should decide and follow a convention that works for you* don't worry too much about what the global convention is. You'd rather have something that makes sense to you* to remove confusion if there is any or grouped together in a way that makes sense.
The most important thing here is you want to achieve a convention/pattern/ruleset that HELPS you: The biggest aid is to have things organised and named in a way that's easy to follow without having to open and read the file's contents before doing anything.
For example: We have Services in a folder called "services" and services that call API's we have in a folder called "gateways". So using our example we'd have /services/UserService.ts and /gateways/UserGateway.ts, that makes it easier to differentiate and it describes what each "User*" thing does.
*and your team if any.
1
u/Salketer Mar 25 '26
We use stuff.backend.ts here.
Worth mentioning that all our backend services are autogenerated from openapi doc. Makes it easy to know that file shouldnt be modified etc.
1
u/fitigued Mar 25 '26
I call all my APIs from a file called `data.service.ts` in the `services` folder. I've used this approach on many large projects and it has never become unwieldy.
1
u/elZaphod Mar 26 '26
I believe it’s more helpful to have descriptive folder names, with concise file names.
1
u/Dus1988 Mar 27 '26
Names like UserService are the reason angular team has removed having Service in the class/file names. They seem to think that removing that, will encourage devs to name things like user-api.ts and UserApi, which in their theory will make naming things more "intentional" and descriptive
Personally as someone who has always used naming like UserApiService and ManageUsersViewService, I hate the new style guide rec here. I find value in having the class and file name having the entity type in it. But it's not a replacement for a good descriptive name.
I'd recommend the UserApiService or UserApi (if following the new style guide). Even If you put the file in a API directory, when you import it to other files, it will be more readily apparent that what you are importing is a class that handles API calls.