例如,我正在尝试导出TS对象以获取以下JavaScript输出:
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];
这是我的TS,但导出尚不明确,
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]但是它的生成的输出是不期望的。我究竟做错了什么?
export default config[env]
在ES6中,允许使用导出功能导出名称,或者默认情况下,您可以导出任何内容。该require格式是这样的:
require
let config = require('config')
并且它将默认导出配置文件。对于您的情况,您应该执行以下操作:
如果要使用导出,请执行以下操作:
let Environment = config[env]; export {Environment}
区别是:
import EnvirmentNameWhatever from "./config"
至
import {Environment} from "./config"