使用枚举形式setInterval()导致异常?

时间:2018-02-11 07:54:50

标签: javascript typescript

我想使用-v "C:/Program Files/MySQL/Data/":/var/lib/mysql/ 中的enum e.g:

setInterval()

如果我得到例外:

declare global {
    enum En_Sch_Mode {
        Daily = 1,
        Manually = 4,
        Sensor = 5
    }
}

export class DeviceInfoEx {
 private m_time_handel: any;
 private m: DeviceInfo;
    constructor() {
        this.timer_check = this.timer_check.bind(this);
        this.m_time_handel = setInterval(this.timer_check, 1000);
    }

    public timer_check(): void {
        if (this.m.schedulemode === En_Sch_Mode.Daily) {
            console.log("hello world");
        }
    }
}

2 个答案:

答案 0 :(得分:0)

declare表示在javascript中有一个enum En_Sch_Mode定义。 像这样:

var En_Sch_Mode;
(function (En_Sch_Mode) {
    En_Sch_Mode[En_Sch_Mode["Daily"] = 1] = "Daily";
    En_Sch_Mode[En_Sch_Mode["Manually"] = 4] = "Manually";
    En_Sch_Mode[En_Sch_Mode["Sensor"] = 5] = "Sensor";
})(En_Sch_Mode || (En_Sch_Mode = {}));

所以也许你想要:

enum En_Sch_Mode {
    Daily = 1,
    Manually = 4,
    Sensor = 5
}

// Your "if" compiles in JavaScript to:
if (this.m.schedulemode === En_Sch_Mode.Daily) {

const enum En_Sch_Mode {
    Daily = 1,
    Manually = 4,
    Sensor = 5
}

// Your "if" compiles in JavaScript to:
if (this.m.schedulemode === 1 /* Daily */) {

答案 1 :(得分:0)

按如下方式定义枚举:

export enum En_Sch_Mode {
    Daily = 1,
    Manually = 4,
    Sensor = 5
}

Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.

如果您要查找命名空间,请按以下步骤操作:

namespace myNameSpace{
    export enum En_Sch_Mode {
        Daily = 1,
        Manually = 4,
        Sensor = 5
    }
}

let enSchMode = myNameSpace.En_Sch_Mode.Daily;
console.log(enSchMode);

您可以在此测试您的打字稿有效期: https://www.typescriptlang.org/play/index.html