我正在将现有的JavaScript项目转换为类型脚本。我不知道如何导出对象来获取此javscript输出
const path = require('path'),
rootPath = path.normalize(__dirname + '/..'),
env = process.env.NODE_ENV || 'development';
let config = {
development: {
amqpUrl: "amqp://localhost:15672",
root: rootPath
},
test: {
amqpUrl: "amqp://localhost:5672",
root: rootPath
},
production: {
amqpUrl: "amqp://localhost:5672",
root: rootPath
}
};
module.exports = config[env];
我写了如下的打字稿,但不清楚导出
import path = require("path")
const rootPath = path.normalize(__dirname + '/..')
const env = process.env.NODE_ENV || 'development'
let config = {
development: {
amqpUrl: "amqp://localhost:15672",
root: rootPath
},
test: {
amqpUrl: "amqp://localhost:5672",
root: rootPath
},
production: {
amqpUrl: "amqp://localhost:5672",
root: rootPath
}
};
/* this is the line i'm having problem how can i export config object*/
// export config[env];
当我导出时如何导出我尝试使用export default config[env]
的对象,但它会从预期输出中产生一些偏差
答案 0 :(得分:15)
在ES6中,您可以使用导出功能导出名称,或者默认情况下您可以导出任何内容。
require
格式如下:
let config = require('config')
它需要配置文件的默认导出。 在你的情况下,你应该这样做:
export default config[env]
如果您想使用导出,您可以执行以下操作:
let Environment = config[env];
export {Environment}
区别在于:
import EnvirmentNameWhatever from "./config"
到
import {Environment} from "./config"
答案 1 :(得分:10)
在要导出的声明上使用export
关键字应该完成这项工作,如下所示:
import path = require("path")
const rootPath = path.normalize(__dirname + '/..')
export const env = process.env.NODE_ENV || 'development'
export let config = {
development: {
amqpUrl: "amqp://localhost:15672",
root: rootPath
},
test: {
amqpUrl: "amqp://localhost:5672",
root: rootPath
},
production: {
amqpUrl: "amqp://localhost:5672",
root: rootPath
}
};