在JavaScript中,我使用如下常量:
module.exports = {
ACTIONS: {
ACTION1: "Action",
ACTION2: ...
}
TEST: "test",
...
};
然后像这样使用它:
var Constants = requre(../utils/constants.js);
console.log(Constants.ACTIONS.ACTION1); //Action
console,log(Constants.TEST); //test
有没有办法在Typescript中使用类似的结构?
您可以在TypeScript中执行相同的操作:
// utils/constants.ts
export = {
ACTIONS: {
ACTION1: "Action"
},
TEST: "test"
};
然后在另一个文件中:
import Constants = require("./../utils/constants");
console.log(Constants.ACTIONS.ACTION1); // Action
console.log(Constants.TEST); // test
ES6进口
如果您使用的是ES6导入,则可以执行以下操作:
export default {
ACTIONS: {
ACTION1: "Action"
},
TEST: "test"
};
然后:
import Constants from "./../utils/constants";
// use Constants here
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句