Skip to content

Constant常量

模块可以单独提供自己的 Constant 常量

定义Constant

以模块a-demo为例,定义模块的 Constant 常量:

src/suite/a-demo/modules/a-demo/src/config/constants.ts

typescript
export const constants = {
  gender: {
    male: 1,
    female: 2,
  },
};
  • 直接定义所需要的常量即可,系统会自动提取 Constant 的类型信息

使用Constant

可以通过 Scope 实例获取模块的 Constant 常量

typescript
export class TestA {
  protected async __init__() {
    const male = this.scope.constant.gender.male;
    const female = this.scope.constant.gender.female;
    console.log(male, female);
  }
}
  • 动图演示 scope-constant

跨模块使用Constant

typescript
import type { ScopeModuleADemo } from 'zova-module-a-demo';

export class TestA {
  @UseScope('a-demo')
  scopeModuleADemo: ScopeModuleADemo;

  protected async __init__() {
    const male = this.scopeModuleADemo.constant.gender.male;
    const female = this.scopeModuleADemo.constant.gender.female;
    console.log(male, female);
  }
}

基于 MIT 许可发布