角类的文件类型命名约定 [英] Filetype Naming Convention for Angular Classes

查看:42
本文介绍了角类的文件类型命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular的样式指南列出了常见文件类型的命名约定,例如如 heroes.component.ts heroes.service.ts 等.但是,没有装饰符的类又如何呢?例如,《英雄》教程创建了一个英雄类:

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? For example, the Heroes tutorial creates a Hero class:

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

然后将其命名为 hero.ts .这样的命名约定在 app 目录中似乎容易出现混乱.创建一个名为classes的文件夹(在app目录中),然后向每个文件添加一个 class.ts 后缀,例如 hero.class.ts ,似乎是合乎逻辑的.

It then names this file hero.ts. A naming convention like this seems prone to chaos in the app directory. 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.

但是,我还没有看到任何人使用此约定.我见过人们使用 model.ts 后缀,但是目前尚不清楚这是否是最佳做法,并且在Angular

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.

有人可以阐明这个话题吗?抱歉,如果我错过了任何事情.谢谢你提前.

Can anyone shed some light on this topic? Sorry if I've missed anything. Thank you advance.

推荐答案

Angular Style Guide明确表示要对简单模型使用 .model .但是,您应该考虑两件事:

Angular Style Guide explicitly says to use .model for simple models. However, you should consider 2 things:

  • 枚举没有约定.我个人将它们保留在 service model 文件中,具体取决于应该如何使用
  • 指南允许创建其他实体,因此在您的项目范围内-您可以添加其他类型"(资源,配置)
  • 如果在整个应用程序中使用类/模型/接口(例如 User ),则指南建议将其保留在 shared 模块中,这在逻辑上是正确的: SharedModule应该多次导入,而 CoreModule -仅导入一次(在 AppModule 中)
  • there is no convention for enums. Personally I keep them inside of service or model file, depending on how it supposed to be used
  • guide allows to create other entities, so in scope of your project - you can add other "types" (recource, config)
  • if class/model/interface (e.g. 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)

这篇关于角类的文件类型命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆