[英]Filetype Naming Convention for Angular Classes
Angular's Style Guide lists naming conventions for common file types, such as heroes.component.ts
, or heroes.service.ts
, etc. But what about classes that have no decorator symbols? Angular的样式指南列出了常见文件类型的命名约定,例如
heroes.component.ts
或heroes.service.ts
等。但是那些没有装饰器符号的类呢? For example, the Heroes tutorial creates a Hero class: 例如,Heroes教程创建了一个Hero类:
export class Hero {
id: number;
name: string;
}
It then names this file hero.ts
. 然后它将此文件
hero.ts
A naming convention like this seems prone to chaos in the app
directory. 像这样的命名约定似乎在
app
目录中容易出现混乱。 It seems logical to create a folder named classes (in the app directory), and then add a class.ts
suffix to each file, such as hero.class.ts
. 创建名为classes的文件夹(在app目录中),然后为每个文件添加
class.ts
后缀(例如hero.class.ts
)似乎是合乎逻辑的。
However, I have not seen anyone using this convention. 但是,我还没有看到有人使用这个约定。 I've seen people use a
model.ts
suffix, but it's unclear whether or not this is a best practice, and it is not mentioned in the Angular Style Guide . 我见过人们使用
model.ts
后缀,但不清楚这是否是最佳实践,并且在Angular Style Guide中没有提到。
Can anyone shed some light on this topic? 任何人都可以对这个话题有所了解吗? Sorry if I've missed anything.
对不起,如果我错过了什么。 Thank you advance.
谢谢你提前。
Angular Style Guide explicitly says to use .model
for simple models. Angular Style Guide明确表示将
.model
用于简单模型。 However, you should consider 2 things: 但是,你应该考虑两件事:
enums
. enums
没有惯例。 Personally I keep them inside of service
or model
file, depending on how it supposed to be used service
或model
文件中,具体取决于它应该如何使用 entities
, so in scope of your project - you can add other "types" (recource, config) entities
,因此在项目范围内 - 您可以添加其他“类型”(recource,config) User
) is used across application, guide suggests to keep it in shared
module, which is logically right: SharedModule
is supposed to be imported multiple times, while CoreModule
- only once (in AppModule
) User
),指南建议将其保存在shared
模块中,这在逻辑上是正确的: SharedModule
应该被多次导入,而CoreModule
- 只有一次(在AppModule
) There are two ways of creating packages (folders) in your application: 在应用程序中有两种创建包(文件夹)的方法:
Angular prefers "package by feature" and that's why hero class remains within hero component which it belongs to. Angular更喜欢“逐个功能”,这就是为什么英雄级别仍然属于它所属的英雄组件。 Good post about why "package by feature" is preferred way is here .
关于为什么“按功能包装”是首选方式的好帖子就在这里 。
As for classes/services which are used for several components - Angular StyleGuide suggests to use core package. 至于用于多个组件的类/服务 - Angular StyleGuide建议使用核心包。 If it's a component which is shared - then a shared package.
如果它是一个共享的组件 - 那么一个共享包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.