typescript 如何在打字稿和角度中定义静态常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53163595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:42:24  来源:igfitidea点击:

how to define static constants in typescript and angular

angulartypescriptstaticconst

提问by Janier

I have bunch of constants which are business related and I have to store them in Angular.Something like below

我有一堆与业务相关的常量,我必须将它们存储在 Angular.Something 中,如下所示

STUDENT_NAMES= ["JOHN","BOB","NICK"]
TEACHER_NAME = ["HARRY","CHRIS"]
 SCHOOL_CODE = [100,102,107];

I have a lot of them and they are kind of static ..I need them in most of my service classes. What is the best place to store them? Should i create a interface and let my service class inherit them? In java , we define a class of public static final constants and other classes uses them.Like this , what is the typescript way?

我有很多它们,它们是静态的……我在大多数服务类中都需要它们。存放它们的最佳位置是什么?我应该创建一个接口并让我的服务类继承它们吗?在java中,我们定义了一个public static final常量的类,其他类使用它们。像这样,打字稿的方式是什么?

回答by pzaenger

Define an abstract class for your constants:

为常量定义一个抽象类:

export abstract class Constants {
  static readonly STUDENT_NAMES: string[] = ["JOHN", "BOB", "NICK"];
  static readonly TEACHER_NAME: string[] = ["HARRY", "CHRIS"];
  static readonly SCHOOL_CODE: number[] = [100, 102, 107];
}

Then include this class whereever needed with import { Constants } from '...';and use its values with const names: string[] = Constants.STUDENT_NAMES;

然后在任何需要的地方包含这个类,import { Constants } from '...';并使用它的值const names: string[] = Constants.STUDENT_NAMES;

Regarding the naming I agree with @AdrianBrand to prefer names like studentNames, teacherNamesand schoolCodes.

关于命名我同意@AdrianBrand 更喜欢像studentNames,teacherNamesschoolCodes.

Edit:TypeScript 3.4 introduced so called constassertions, which might also be suited:

编辑:TypeScript 3.4 引入了所谓的constassertions,它也可能适合:

export const constants = {
  studentNames: ["JOHN", "BOB", "NICK"],
  ...
} as const;

回答by Adrian Brand

You put them in a TypeScript file and import them where needed.

你把它们放在一个 TypeScript 文件中,并在需要的地方导入它们。

export const STUDENT_NAMES: string[] = ["JOHN","BOB","NICK"];

Put it in student-names.ts

把它放在 student-names.ts

import { STUDENT_NAMES } from './path/cosnstants/student-names';

where you need it.

你需要的地方。

Personally I would name them studentNames and not STUDENT_NAMES but that is a matter of taste.

就个人而言,我会将它们命名为 studentNames 而不是 STUDENT_NAMES,但这是一个品味问题。

回答by Mohamed Ali RACHID

You can create a TS file constants.tsthat contains all your constants :

您可以创建一个constants.ts包含所有常量的 TS 文件:

export const constants = {
   STUDENT_NAMES: ["JOHN","BOB","NICK"],
   TEACHER_NAME: ["HARRY","CHRIS"],
   SCHOOL_CODE: [100,102,107]
};

Then whenever you need a constant you can call it like this code bellow:

然后,每当您需要一个常量时,您都可以像下面的代码一样调用它:

let studentsNames : any = constants.STUDENT_NAMES;

Regards,

问候,

回答by Rahul

Add all your constants in environment.tsfile - this is the best practice and there will be no change in the values basd on the environment specific

environment.ts文件中添加所有常量- 这是最佳实践,基于特定环境的值不会发生变化

Add both in environment.tsand environment.prod.ts- this will be your global object and can be accessed at any level

添加environment.tsenvironment.prod.ts- 这将是您的全局对象,可以在任何级别访问

Importing will be same as importing an interface or any class Thanks - Happy coding

导入将与导入接口或任何类相同 谢谢 - 快乐编码

回答by Jayampathy Wijesena

you could try enums, this will allow you to define a set of named constants. Refer to this document.

您可以尝试枚举,这将允许您定义一组命名常量。请参阅此文档。

enum Constants {
    STUDENT_NAMES= ["JOHN","BOB","NICK"]
    TEACHER_NAME = ["HARRY","CHRIS"]
    SCHOOL_CODE = [100,102,107];
}