如何将 Typescript 枚举的值添加到我的 JavaScript 函数中

How can I add the value of a Typescript enum to my javascript function?

本文关键字:我的 JavaScript 函数 添加 Typescript 枚举      更新时间:2023-09-26

我创建了这个appRun.ts文件:

/// <reference path="app.ts"/>
/// <reference path="services/EnumsService.ts"/>
app.run(['$rootScope', appRun]);  
function appRun($rootScope) {
    $rootScope.Action = Action;
    $rootScope.AuthTabs = AuthTabs;
    $rootScope.UserTestStatus = UserTestStatus;
    $rootScope.Network = Network;
    $rootScope.TS = TS;
}

在 EnumsService.ts 中:

enum Action {
    None,
    Registering,
    Authenticating
}

但是当涉及到我的Javascript AppRun时.js它被创建时对Action枚举一无所知。

/// <reference path="app.ts"/>
/// <reference path="services/EnumsService.ts"/>
app.run(['$rootScope', appRun]);
function appRun($rootScope) {
    $rootScope.Action = Action;
    $rootScope.AuthTabs = AuthTabs;
    $rootScope.UserTestStatus = UserTestStatus;
    $rootScope.Network = Network;
    $rootScope.TS = TS;
}
//# sourceMappingURL=appRun.js.map

如果您使用的是内部模块,则需要...

  1. 确保所有脚本在运行时按顺序加载(通常通过按适当的顺序将所有脚本标记添加到页面)

  1. 编译为单个文件,以便包含所有脚本。如果正确设置了参考注释,则将对单个文件进行排序

编译为单个文件的示例

tsc --out combined.js appRun.ts

如果您使用的是 Visual Studio,也可以在项目设置中选择此选项。