我想使用 setInterval()
中的 enum
。
例如:
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");
}
}
}
如果我遇到异常:
if (this.m.schedulemode === En_Sch_Mode.Daily)
Exception has occurred: ReferenceError
ReferenceError: En_Sch_Mode is not defined
at DeviceInfoEx.timer_check (d:\tfshomeprojects\Node.js\smartpow_ts\src\model\DeviceInfoEx.ts:80:37)
at ontimeout (timers.js:475:11)
at tryOnTimeout (timers.js:310:5)
at Timer.listOnTimeout (timers.js:270:5)
最佳答案
按如下方式定义枚举:
export enum En_Sch_Mode {
Daily = 1,
Manually = 4,
Sensor = 5
}
全局作用域的增强只能直接嵌套在外部模块或环境模块声明中。
如果您正在寻找 namespace ,请按以下步骤操作:
namespace myNameSpace{
export enum En_Sch_Mode {
Daily = 1,
Manually = 4,
Sensor = 5
}
}
let enSchMode = myNameSpace.En_Sch_Mode.Daily;
console.log(enSchMode);
您可以在此处测试您的 typescript 有效性: https://www.typescriptlang.org/play/index.html
关于javascript - 使用枚举形式 setInterval() 导致异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48729499/